PDA

View Full Version : Unix Timestamp to dd-mm-yyyy


neilo
07-09-2011, 08:32 PM
Hi,

I have a recordset from which I query:

$query_rs_data = "SELECT time_last_run FROM phpmysqlautobackup";
which I use to output a Date:

<?php echo $row_rs_data['time_last_run']; ?>
The 'time_last_run' value is a Unix timestamp, i.e: 1310250618 (seconds from start of Unix epoch).

How can I output this to page as dd/mm/yyyy instead of 1310250618 (without it outputting 1st Jan 1970).

Help appreciated.

neilo
07-09-2011, 09:37 PM
Thanks to Dave Buchholz (yet again) whose post I found Here (http://www.webassist.com/forums/showthread.php?t=10791&highlight=Unix+timestamp)

So for my example above, it was:

$query_rs_data = "SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL time_last_run SECOND), '%b %d %Y %h:%i %p') As LASTRUN FROM phpmysqlautobackup";
and

<?php echo $row_rs_pg['LASTRUN']; ?>
which output: Jul 09 2011 11:34 PM

Perfect. Cheers Dave.

Jason Byrnes
07-11-2011, 08:05 AM
just to keep your options open, another way to accomplish this is using the php date function (http://php.net/manual/en/function.date.php):

example:

<?php echo(date("d/m/Y", $row_rs_data['time_last_run'])); ?>

neilo
07-11-2011, 10:39 AM
Hi Jason,

Thanks, - I tried this (as is, and in variations thereof) but this kept returning 01/01/1970.

Jason Byrnes
07-11-2011, 10:44 AM
perhaps try:

<?php echo(date("d/m/Y", strtotime($row_rs_data['time_last_run']))); ?>

neilo
07-11-2011, 11:01 AM
Yes, I'd have thought that would do it, but it still returned 01/01/1970

Just echoing the value; <?php echo $row_rs_test['time_last_run']; ?>

returns the unix date (in seconds from Unix beginning date): 1310250898

Using the regular php date functions doesn't seem to increment this value on to the Unix (epoch start) date and print it as a recognizable date format.

Jason Byrnes
07-11-2011, 11:08 AM
not at all sure what the problem may be.

This code, for me:
<?php echo(date("d/m/Y", "1310250618")); ?>


returns:
09/07/2011

neilo
07-11-2011, 11:31 AM
The problem is the same one that keeps cropping up, i.e. - me.

Running it again on a fresh test page (with recordset rs_test) with your first code:

<?php echo(date("d/m/Y", $row_rs_test['time_last_run'])); ?>

Returns the date just fine. I now need to back-track and see how I messed up the first time.

I think it is my destiny to make others feel better about themselves and their abilities by displaying my ignorance on these pages. Thanks for helping out - sorry to have wasted your time again,

n.

Jason Byrnes
07-11-2011, 12:57 PM
No worries, glad to hear you got it working.