Jump to content

Recommended Posts

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"));

Link to comment
https://forums.phpfreaks.com/topic/218119-simple-datetime-question/
Share on other sites

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.

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

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.

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

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.

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.