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?
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?
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
Here's something that you can refer from: http://www.daniweb.com/forums/thread85726.html
Can you do the add as it comes out of the database?
SELECT ... DateAdd(hh, 13, DateField) AS DateField
(SQL Server)
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.
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'));
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"));
<?php
echo date('Y-m-d H:i:s', strtotime('+13 hours', strtotime('2009-07-14 02:00:00')));
?>