Extract Time component from DateTime field using PHP

Daniel Pineault
Daniel Pineault used Ask the Experts™
on
I'm trying to extract the Time from a DateTime field, but when I do so I always get 7:00 PM or 19:00 depending on the format I choose and I can't figure out why.

echo $modelLeg->StartDtTime.' '.strtotime($modelLeg->StartDtTime).' '.date("H:i:s", strtotime($modelLeg->StartDtTime))

Open in new window


This returns

1899-12-30 00:00:00 19:00:00
1899-12-30 00:00:00 19:00:00
1899-12-30 09:30:00 19:00:00
1899-12-30 09:30:00 19:00:00
1899-12-30 11:00:00 19:00:00
1899-12-30 08:00:00 19:00:00
1899-12-30 08:00:00 19:00:00

So, strtotime() recognizes the Time properly, but when I add in the date("H:i:s"...) aspect it errs for some reason.  Could someone explain why and what I am doing wrong.

Thank you!


Update:
I kept playing, researching and I have managed to get it to work by doing

$date = DateTime::createFromFormat('Y-m-d H:i:s', $modelLeg->StartDtTime);
echo $date->format("g:i A");

Open in new window


but as a novice PHP programmer I'd still like to understand and learn why what I was doing previous wasn't working and if this new code is truly a good solution.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hi there,

The problem you're having is down to the date you're using. When you call strtotime() it tries to convert the date to an integer data type, representing the number of seconds before NOW. The integer datatype has a pre-set range - approximately negative 2 billion to positive 2 billion (-2,147,483,648 to 2,147,483,647). Any dates before 2,147,483,648 seconds ago, (currently about 1901-12-14) will try to convert to an integer outside of that range, and therefore the strtotime function will return false. You can see this in action with the following:

echo strtotime("1899-12-30 00:00:00"); // this will output 'false'
echo strtotime("1901-12-30 00:00:00"); // this will out a negative integer

Open in new window

In your echo statement, this means that nothing will be output when you call strtotime($modelLeg->StartDtTime). Because your strtotime function returns false, you pass false into the date() function, which will return the default date of 1970-01-01 00:00. You then try and format that to H:i:s. Given your timezone, this is why you're always getting 19:00 (I'm guessing you're in a GMT-5 timezone).

Bottom line is that strtotime will only work on dates later than 2,147,483,648 seconds ago. The createFromFormat() function doesn't care about timestamps (integer datatype), which is why it works.

Because your stored datetime is formatted to the default datetime (which is good), then you don't actually need the createFromFormat. You can just create a date directly from your value:

$date = new DateTime($modelLeg->StartDtTime);
echo $date->format("g:i A");

Open in new window

Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018

Author

Commented:
Thank you for the solution, but more importantly the explanation!
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No worries :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial