webmaster1 Posted April 18, 2010 Share Posted April 18, 2010 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. 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? Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/ Share on other sites More sharing options...
webmaster1 Posted April 18, 2010 Author Share Posted April 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044006 Share on other sites More sharing options...
oni-kun Posted April 18, 2010 Share Posted April 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044007 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2010 Share Posted April 18, 2010 Just use the mysql DATE_FORMAT() function in your query, that's what it is for. I would also use an alias name so that you can simply reference the results by the alias - $min= $rowmin['alias_name_you_choose']; Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044011 Share on other sites More sharing options...
webmaster1 Posted April 18, 2010 Author Share Posted April 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044013 Share on other sites More sharing options...
webmaster1 Posted April 18, 2010 Author Share Posted April 18, 2010 Just use the mysql DATE_FORMAT() function in your query, that's what it is for. I would also use an alias name so that you can simply reference the results by the alias - $min= $rowmin['alias_name_you_choose']; Trying this now, cheers. Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044014 Share on other sites More sharing options...
joel24 Posted April 18, 2010 Share Posted April 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044015 Share on other sites More sharing options...
webmaster1 Posted April 18, 2010 Author Share Posted April 18, 2010 @PFMaBiSmAd / joel24: Thanks for breaking that down. I'm now up to speed with the query and strtotime approach. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044018 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2010 Share Posted April 18, 2010 but the mysql date format isn't compatible with that function The msyql date format is compatible with the strtotime() function, as demonstrated in the code in Reply #4 in this thread. Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044019 Share on other sites More sharing options...
webmaster1 Posted April 18, 2010 Author Share Posted April 18, 2010 The msyql date format is compatible with the strtotime() function, as demonstrated in the code in Reply #4 in this thread. Concurred. Quote Link to comment https://forums.phpfreaks.com/topic/198890-cannot-output-datetime-values-from-table-correctly/#findComment-1044024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.