Extract Time component from DateTime field using PHP

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.
LVL 1
D PineaultAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
D PineaultAuthor Commented:
Thank you for the solution, but more importantly the explanation!
0
Chris StanyonWebDevCommented:
No worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.