PHP: replacement for DateTime object
The server that I have my sited hosted is on PHP5.12.14, and I have an error when I run the DateTime object from PHP5.3
# DateTime::add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
$date = new DateTime($item_user['mem_updated']);
# add a day to the object
$date -> add(new DateInterval('P1D'));
the error,
Fatal error: Call to undefined method DateTime::add() in /homepages/xxx.php on line xx
So, I have look for the other solutions rather than sticking to PHP5.3's DateTime object. How can I write the code to replace the cod开发者_如何学JAVAe above?
basically I have this date and time data (for instance - 2011-01-21 02:08:39
) from the mysql database, and I just need to add 1 day or 24 hours to that date/time, then passing it into a function below,
$time_togo = time_togo($date -> format('Y-m-d H:i:s'));
thanks.
strtotime would work
$timestamp = strtotime($item_user['mem_updated']);
$time_togo = date("Y-m=d H:i:s", strtotime("+1 Day", $timestamp));
Here's an example:
$date = date('Y-m-d H:i:s');
$new_tstamp = strtotime($date.'+1WEEK');
$new_date = date('Y-m-d H:i:s', $new_tstamp);
In other words, strtotime lets you use date expressions like +1DAY
, +1MONTH
and so on.
The above will work for date string (e.g.: 2010-01-01). If your original date is a Unix timestamp, you can still use strtotime
, although a bit differently:
$new_tstamp = strtotime('+1WEEK', $timestamp);
精彩评论