Log in

View Full Version : how to display MySQL timestamp?



jr_yeo
01-29-2009, 11:05 AM
ive got database with a date field. when i extracted the data, the format is something like "2003-12-25 23:59:59". how do i "reformat" it to something like
"Thursday, December 25, 2003 23:59". i tried

echo date("F j Y H:i:s T", $row["date"]);

the date/time outputted resets to January 1 1970 00:33:29 UTC.

tnx.

Nile
01-29-2009, 10:54 PM
Here you go:


function displayToMk($date){
$date = explode(" ", $date);
$date[0] = explode("-", $date[0]);
$date[1] = explode(":", $date[1]);
return mktime($date[1][0], $date[1][1], $date[1][2], $date[0][1], $date[0][2], $date[0][0]);
}
echo date("l, F j, Y H:i", displayToMk($row["date"));

Twey
01-30-2009, 02:03 PM
By chaining it through strptime (http://www.php.net/strptime)() and then back through date (http://www.php.net/date)():
date('F j Y H:i:s T', strptime('%Y-%m-%d %T', $row['date']));

jr_yeo
01-31-2009, 08:54 PM
Fatal error: Call to undefined function strptime()

Schmoopy
01-31-2009, 08:57 PM
Make sure your server is not running on Windows, or it won't work.

jr_yeo
01-31-2009, 09:11 PM
Make sure your server is not running on Windows, or it won't work.

why won't it work?

Schmoopy
01-31-2009, 09:23 PM
strptime (http://uk3.php.net/strptime) : Scroll a bit down the page




Note: This function is not implemented on Windows platforms.

Nile
02-01-2009, 12:57 AM
Mine will work on Windows. :)

Although if you're using this on a website and your website isn't hosted on a windows server then go ahead and try Twey's on your site, and not a localhost,

jr_yeo
02-01-2009, 02:57 AM
strptime (http://uk3.php.net/strptime) : Scroll a bit down the page.
Note: This function is not implemented on Windows platforms.

it doesn't still explain why it won't work in windows. :cool:

jr_yeo
02-01-2009, 04:06 AM
tried Twey's on Linux.

how do i implement it now?


$query1 = "SELECT date FROM $tablename";
$result = mysql_query($query1) or die('Error, query failed');
while($row = mysql_fetch_array($result)) {
echo '<td align=right>' . date('F j Y H:i:s T', strptime('%Y-%m-%d %T', $row['date'])) . '</td></tr>'. "\n";
}

the date still reverts back to January 1 1970 not the CURRENT_TIMESTAMP :o

Nile
02-01-2009, 04:07 AM
function displayToMk($date){
$date = explode(" ", $date);
$date[0] = explode("-", $date[0]);
$date[1] = explode(":", $date[1]);
return mktime($date[1][0], $date[1][1], $date[1][2], $date[0][1], $date[0][2], $date[0][0]);
}
$query1 = "SELECT date FROM $tablename";
$result = mysql_query($query1) or die('Error, query failed');
while($row = mysql_fetch_array($result)) {
echo '<td align=right>' . date('F j Y H:i:s T', displayToMk($row["date")) . '</td></tr>'. "\n";
}

jr_yeo
02-01-2009, 04:15 AM
@Schmoopy


Make sure your server is not running on Windows, or it won't work.


tried Twey's on Linux.

how do i implement it now?


$query1 = "SELECT date FROM $tablename";
$result = mysql_query($query1) or die('Error, query failed');
while($row = mysql_fetch_array($result)) {
echo '<td align=right>' . date('F j Y H:i:s T', strptime('%Y-%m-%d %T', $row['date'])) . '</td></tr>'. "\n";
}

the date still reverts back to January 1 1970 not the CURRENT_TIMESTAMP :o

dont worry Nile, yours works just fine in both platforms :)

Nile
02-01-2009, 04:20 AM
I'm saying - use mine, not one that will work on all servers.

By any change do you know if your server is hosted on Windows, Linux, or Mac?

jr_yeo
02-01-2009, 05:10 AM
i did use your code :D

m localhost on Windows and Linux. :cool:

but does it matter if mine is locally hosted and need to be on remote host/server?

Twey
02-01-2009, 04:25 PM
Sorry, I got the arguments the wrong way around. It should be date('F j Y H:i:s T', strptime($row['date'], '%Y-%m-%d %T')).


it doesn't still explain why it won't work in windows.Well, yes, it does. The function isn't implemented there, therefore it can't.

A better solution to this would be to implement strptime() in PHP if it doesn't already exist, though it seems a bit tedious. It's fairly simple to parse out the assorted values, but then there are about fifty different scales to apply, which has to be done manually as far as I can see.

I see that strptime() is not a strict inverse of strftime(). While strftime() takes a time_t/timestamp, strptime() returns a struct/array of various values. This does make it easier to implement, but unfortunately it also means that it's less useful for your situation.

jr_yeo
02-01-2009, 07:33 PM
Warning: date() expects parameter 2 to be long, array given in my filename.php