Currently my code is adding a week, but I'm using Days instead of Weeks. I've read the documentation and don't quite understand how it works.
PHP: DateInterval
# Adds 7 days to the project launch date.
$project_launch_date->add(new DateInterval('P7D'));
Instead of adding manual 7 days, how can I specify, 'add a week', or 'add n weeks'?
php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("next Sunday"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date) .
To add days to DateValue , you can use DateInterval. Day , DateInterval. DayOfYear , or DateInterval.
PHP | DatePeriod getDateInterval() Function The DatePeriod::getDateInterval() function is an inbuilt function in PHP which is used to return the date interval for the given date period. Syntax: DateInterval DatePeriod::getDateInterval( void )
You can use strtotime. $your_date = strtotime("1 day", strtotime("2016-08-24")); $new_date = date("Y-m-d", $your_date);
If DateInterval is unclear for you, you can use more clear modify of DateTime class.
$date = new DateTime();
$date->modify('+1 day');
$date->modify('+5 week');
I prefer to use modify, because it makes code more readable without comments
In case you prefer to use DateInterval, here is good reference: http://www.php.net/manual/en/dateinterval.construct.php
So 5 weeks will be P5W, 3 month will be P3M, 5 weeks AND 3 month P3M5W and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With