Time in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hiver75
Forum Newbie
Posts: 6
Joined: Mon Oct 20, 2014 12:31 pm

Time in PHP

Post by Hiver75 »

I'm trying to write PHP code that grabs a time from the database using PDO and then finds out how much time has past between it and the present.

How would you do that? Everytime I've tried it's given me errors.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Time in PHP

Post by Christopher »

You don't say what kind of database you are using, or what type of fields you are using. Databases have lots of functions for dates, such as DATEDIFF(), TIMESTAMPDIFF() or TIMEDIFF() ... MySQL: http://dev.mysql.com/doc/refman/5.6/en/ ... tions.html
(#10850)
Hiver75
Forum Newbie
Posts: 6
Joined: Mon Oct 20, 2014 12:31 pm

Re: Time in PHP

Post by Hiver75 »

I'm using MySQL.

That seems to be a list of MySQL date commands, I can already grab the date from the DB fine enough, it's just when it comes to finding the difference between two dates in PHP, it doesn't work directly from the DB. All the other questions I've seen asked on this involve the date in plain text, not grabbing the date from the DB.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Time in PHP

Post by Christopher »

Then use the PHP DateTime and related classes: http://php.net/manual/en/book.datetime.php
(#10850)
Post Reply