I have a scenario where I have to add x number if days to a variable containing date. This x will be dynamic and cannot be guessed.
Any suggestions on how I can implement this?
$ticket_created_on_date_time = '2016-08-31 09:55:01'
$in_between_days = 2;
A quick example:
<?php
$date = new DateTime($ticket_created_on_date_time);
$date->add(new DateInterval(sprintf('P%dD', $in_between_days)));
echo $date->format('Y-m-d H:i:s'); //output: 2016-09-02 09:55:01
More details in http://php.net/manual/en/datetime.add.php
You can use math and the strtotime
function to get a date in the past/future. Something like:
strtotime($ticket_created_on_date_time) + (86400 * $in_between_days)
(86400 is one day in seconds) ....or
strtotime($ticket_created_on_date_time . '+ ' . $in_between_days . ' days')
Demo: https://eval.in/634187
You should be doing it like this,
<?php
$date = new DateTime($ticket_created_on_date_time);
$date->modify("+2 day");
echo $date->format("r");
Edit - You can modify dynamically like this:
$date->modify(sprintf("%u day",$day_diff));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.