Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: replacement for DateTime object

Tags:

php

datetime

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 code 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.

like image 853
Run Avatar asked Mar 08 '26 21:03

Run


1 Answers

strtotime would work

$timestamp = strtotime($item_user['mem_updated']);    
$time_togo = date("Y-m=d H:i:s", strtotime("+1 Day", $timestamp));
like image 181
Damon Skelhorn Avatar answered Mar 11 '26 11:03

Damon Skelhorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!