php date

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
VarwigDude
Forum Newbie
Posts: 12
Joined: Tue Mar 31, 2009 8:08 pm

php date

Post by VarwigDude »

I can't seem to get this. I want to get a date from a mysql row. But i want to add 10 hours to it when I want to display the time.

example
$end_time_and_date = $row['end_time_and_date'] + 10h;
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Re: php date

Post by mickd »

Is it just a mySQL date field? Or datetime field?

If its a date field, it just has YYYY-MM-DD, which could you give an example to what it means to add 10hours to it?

If its a datetime field, YYYY-MM-DD HH:MM:SS, do you want to add 10hours using PHP or during the mySQL query? For PHP, strtotime can convert your mysql time into a PHP timestamp, and then add 36000 (10*60*60) seconds to it.

Code: Select all

 
//$mysqlDate from the mysql database datetime field.
 
$phpDate = strtotime($mysqlDate);
$addTenHours = $phpDate + 36000;
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php date

Post by pickle »

mickd wrote:strtotime can convert your mysql time into a PHP timestamp, and then add 36000 (10*60*60) seconds to it.
It's better to just use strtotime() period - as it takes into account any DST time changes. You can also use the MySQL function UNIXTIMESTAMP() to convert from a MySQL timestamp to a UNIX timestamp right in the query

Code: Select all

$final_stamp = strtotime('+10 hours',$row['unix_timestamp']);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply