Jump to content

Cannot output datetime values from table correctly.


webmaster1

Recommended Posts

I'm outputting the minimum datetime as follows:

 

$querymin = "SELECT MIN(datetime) FROM table where something='$something'"; //query
$resultmin = mysql_query($querymin) or die(mysql_error()); //execute
while($rowmin = mysql_fetch_array($resultmin)){ //loop
$min= $rowmin['MIN(datetime)']; //as variable
//echo $min; 
$min= date("m/d/y g:i (A)", $min); //format date
echo $min;

 

2010-04-18 00:00:53, as stored in my database should be formatted like 18/04/10 00:00 (AM) except it formats as: 01/01/70 1:33 (AM) Notice it's about 4 decades out.  :confused:

 

I've wasted a fair amount of time trouble-shooting this though I keep on bouncing back and forth between unix timestamps and the strftime() function.

 

Can someone please assist? Why is my formatting messing up my date?

Link to comment
Share on other sites

Would making my datetime column into a timestamp make life easier?

 

I was avoiding it because I'm cautious of updating the date when I'm not supposed to.

 

Yes. Note 40 years back was the Unix epoch, There's obviously a flaw in your logical calculations.

Link to comment
Share on other sites

By chance, the following works:

 

$querymin = "SELECT MIN(datetime) FROM table where something='$something'"; 
$resultmin = mysql_query($querymin) or die(mysql_error());

while($rowmin = mysql_fetch_array($resultmin)){
$min= $rowmin['MIN(datetime)'];

$min = strtotime($min);
print date('Y-m-d \a\t H:i', $min)."\n";

 

The strtotime() function through me off a little. A multitude of the examples I encountered used the current time for calculation which I had assumed wouldn't be interchangeable with a variable.

Link to comment
Share on other sites

the date function requires a timestamp, not a m/d/y or datetime date.

 

create the timestamp using strtotime, but the mysql date format isn't compatible with that function, you can use mysql to get the datetime as a unix timestamp, i.e. "SELECT MIN(UNIX_TIMESTAMP(datetime)) FROM table where something='$something'"; //query

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.