phoenixx Posted November 8, 2010 Share Posted November 8, 2010 I'm trying to convert the following time stamp: 12:03PM to military time 13:03. I'm using the following code, but it's just outputing 12:03: $sampledate=("2010-11-08, 12:03PM CST"); $xplodeDateStamp = explode(", ", $sampledate); $xplodeDate = $xplodeDateStamp[0]; $xplodeTime = $xplodeDateStamp[1]; echo "Date: " . $xplodeDate . "<br>"; echo "Time: " . $xplodeTime . "<br>"; echo "Military Time: " . date("H:i",strtotime("$xplodeTime")); Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2010 Share Posted November 8, 2010 1:03 pm is 13:03 in military time/24 hour format, not 12:03 pm. Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131816 Share on other sites More sharing options...
phoenixx Posted November 8, 2010 Author Share Posted November 8, 2010 Okay - oops - here's a more general description of what I need The timestamps I have are g:i AM/PM such as 12:03PM or 3:12AM. I need to convert those to military 24 hr time w/o AM/PM. Sorry about the typo Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131821 Share on other sites More sharing options...
AbraCadaver Posted November 8, 2010 Share Posted November 8, 2010 Okay - oops - here's a more general description of what I need The timestamps I have are g:i AM/PM such as 12:03PM or 3:12AM. I need to convert those to military 24 hr time w/o AM/PM. Sorry about the typo http://us.php.net/manual/en/function.date.php date('G:i', strototime('3:12AM')); Or use H for leading zeros. Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131822 Share on other sites More sharing options...
phoenixx Posted November 8, 2010 Author Share Posted November 8, 2010 Well, it does seem to convert, but it converts 12:03PM to 18:00. Any thoughts? Tried to play w/ it a bit, but no change in output. Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131857 Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2010 Share Posted November 8, 2010 Works as expected for me - echo date('H:i', strtotime('12:03PM')); You would need to post the code that produces the incorrect output for any one here to be able to help you with what it is doing. Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131860 Share on other sites More sharing options...
phoenixx Posted November 8, 2010 Author Share Posted November 8, 2010 Here's the code that outputs 18:00. <? $sampledate=("2010-11-08, 12:03PM CST"); $xplodeDateStamp = explode(", ", $sampledate); $xplodeDate = $xplodeDateStamp[0]; $xplodeTime = $xplodeDateStamp[1]; echo "Date: " . $xplodeDate . "<br>"; echo "Time: " . $xplodeTime . "<br>"; echo "Military Time: " . date('G:i', strtotime('$xplodeTime')); ?> This code outputs: Date: 2010-11-08 Time: 12:03PM CST Military Time: 18:00 Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131861 Share on other sites More sharing options...
AbraCadaver Posted November 8, 2010 Share Posted November 8, 2010 echo '$xplodeTime'; //and echo "$xplodeTime"; //and echo $xplodeTime; Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131865 Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2010 Share Posted November 8, 2010 The reason you are getting a zero minutes value is because you have single quotes around the $xplodeTime variable in the strtotime('$xplodeTime') code and php variables are not replaced with their value when inside of a single-quoted string. The reason you are getting the wrong hour is because you are specifying a time zone in the value (CST) that is different than the time zone setting that php is using and php is adjusting the number of seconds that strtotime() returns to your current time zone setting. Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131867 Share on other sites More sharing options...
phoenixx Posted November 8, 2010 Author Share Posted November 8, 2010 Removing the single quotes did it! Many thanks to all! Here's the final code and output for anyone researching it: $sampledate=("2010-11-08, 4:03PM CST"); $xplodeDateStamp = explode(", ", $sampledate); $xplodeDate = $xplodeDateStamp[0]; $xplodeTime = $xplodeDateStamp[1]; echo "Date: " . $xplodeDate . "<br>"; echo "Time: " . $xplodeTime . "<br>"; echo "Military Time: " . date('G:i', strtotime($xplodeTime)); Outputs: Date: 2010-11-08 Time: 4:03PM CST Military Time: 16:03 Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131869 Share on other sites More sharing options...
AbraCadaver Posted November 8, 2010 Share Posted November 8, 2010 Good catch on the timezone. Worked for me because I'm in CST No need for the explodes either, let the functions work for you: $sampledate = "2010-11-08, 12:03PM"; $timestamp = strtotime($sampledate); echo "Date: " . date('Y-m-d', $timestamp) . "<br>"; echo "Time: " . date('g:i', $timestamp) . "<br>"; echo "Military Time: " . date('G:i', $timestamp); You can use date_default_timezone_set() to force calculations in a specific timezone. Quote Link to comment https://forums.phpfreaks.com/topic/218119-simple-datetime-question/#findComment-1131871 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.