tags:

views:

374

answers:

7

I have a table with timestamp values like:

    2009-07-14 02:00:00

I need to display them at run time with 13 hours added, like:

    2009-07-14 15:00:00

What's the simplest way to do this in PHP?

+1  A: 

you could use http://de3.php.net/manual/en/function.strptime.php to convert it to a unix timestamp. Then you could easily add 13*60*60 to that and use http://de3.php.net/manual/en/function.date.php to convert it back to a timestamp as you like.

anotehr way would be via the explode function, but i think this might be more complicated because you have to look if days/month/years change and stuff

Flo
yeah i don't want to manually do it unless i have to. lemme try the other method you mentioned.
aalaap
+1, thought for date format given, strtotime would work just as well
Paul Dixon
-1, you should not use basic arithmetic for time calculations. Due to Daylight Saving Time some bugs my sneak in. I've wrote about it: http://igstan.blogspot.com/2008/12/how-not-to-make-dates-in-php.html
Ionuț G. Stan
A: 

Here's something that you can refer from: http://www.daniweb.com/forums/thread85726.html

ryanli
it's the same thing as the solution i accepted below, but i'd -1 for linking outside instead of pasting the couple of lines of code here in the answer!
aalaap
+1  A: 

Can you do the add as it comes out of the database?

SELECT ... DateAdd(hh, 13, DateField) AS DateField

(SQL Server)

n8wrl
too costly on the server end, don't you think?
aalaap
IMHO, this one is better as the data is "correct" as soon as it comes back from the query. There's no opportunity to do something else on accident or forget to convert it.
CaseySoftware
+3  A: 

I know that

date( "Y-M-d H:i:s", strtotime( $timestamp_from_array ) + 13 * 3600 );

is smelly, but it will give you an idea.

strtotime converts the timestamp string to a timestamp value, then we add the hours and convert it back to the timestamp format in the array with the date function.

But I suppose what you really want is to use time zones.

Edit: igstan is correct, you should also mind the daylight saving time changes between those offsets.

macbirdie
bingo! this is it!
aalaap
+6  A: 

There are lots of problems with Time Zones and Daylight Saving Time when using basic arithmetic for time calculations. The best solution is to rely on PHP's own date/time functions, like strtotime() or mktime(). I've wrote about DST problems on my blog.

echo date('Y-M-d H:i:s', strtotime('2009-07-14 02:00:00 + 13 hours'));
Ionuț G. Stan
+1  A: 

You have a table with the timestamps? If it's a MySQL database, you could just do this in the database using addtime: SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002');

If you are using the PHP Zend Framework, you could do something like:

 $date=new Zend_Date(Array('year'=>$iYear,'month'=>$iMonth,'day'=>$iDay));
 // changes $date by adding 12 hours
 $date->add('12:00:00', Zend_Date::TIMES);

Alternatively, you could do it using native PHP4 functions such as: [faster, less accurate: doesn't account for leap seconds/time zone changes etc]

$datestamp=strtotime('2009-07-14 02:00:00'); // It'll probably be better using mktime
$datestamp=$datestamp+(60*60*13); // 60 seconds times 60 minutes times 13 hours

or [slower, more accurate]

  $datestamp = strtotime('2009-07-14 02:00:00'); // It'll probably be better using mktime
  $newDate = date('Y-m-d H:i:S', mktime(date('H',$datestamp)+13, date('i',$datestamp), date('s',$datestamp), date('m',$datestamp), date('d',$datestamp), date('Y',$datestamp)));

or, if you are using PHP 5 using the Datetime functions

$date = new DateTime('2009-07-14 02:00:00');
date_add($date, new DateInterval("P13H"));
Richy C.
A: 
<?php
echo date('Y-m-d H:i:s', strtotime('+13 hours', strtotime('2009-07-14 02:00:00')));
?>
David Barnes