PHP: replacing a DateTime object

The server on which I am hosted is hosted on PHP5.12.14, and I have an error when starting 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')); 

error,

Fatal error: Call to undefined method DateTime::add() in /homepages/xxx.php on line xx

So, I was looking for other solutions, not sticking to the PHP5.3 DateTime object. How to write code to replace the above code?

Basically I have date and time data (e.g. 2011-01-21 02:08:39) from the mysql database, and I just need to add 1 day or 24 hours to this date / time, and then pass it to the function below.

$time_togo = time_togo($date -> format('Y-m-d H:i:s'));

thank.

+3
source share
2 answers

strtotime will work

$timestamp = strtotime($item_user['mem_updated']);    
$time_togo = date("Y-m=d H:i:s", strtotime("+1 Day", $timestamp));
+2
source

Here is 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 allows the use of date expressions such as +1DAY, +1MONTHetc.

(: 2010-01-01). Unix, strtotime, -:

$new_tstamp = strtotime('+1WEEK', $timestamp);
+1

Source: https://habr.com/ru/post/1787013/


All Articles