Add days to a date (PHP) [duplicate] - php

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 9 years ago.
I am saving dates like so: 2013-11-06 using date function
date("Y-m-d")
Would like to add three days to the date. Would I need to use strtotime() for this?

Add days to a date (PHP)
$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
you may find here more Date time
Update
How to find time difference between two dates using PHP:
Answer
How to calculate the difference between two dates using PHP?: Answer

Here you go
echo date('Y-m-d', strtotime("+3 days"));

Here, really easy:
$date=date("Y-m-d");
$date_plus_3_days=date("Y-m-d", strtotime("+3 days"));
strtotime can do lots of other related things: PHP.net documentation.

You can also use this.
$date = new DateTime(date("Y-m-d"));
$date->add(new DateInterval('P3D'));
echo $date->format('Y-m-d');

Related

Get the time difference in H:mm:ss format [duplicate]

This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
time difference in HH:MM:SS format
(2 answers)
Closed 5 years ago.
I am trying to find the time difference between two time values in the format H:mm:ss using PHP.
When I tried with the following code, I'm getting the difference 01:00:20 instead of 00:00:20. What's wrong with my code?
$start_time = strtotime("0:17:14");
$end_time = strtotime("0:17:34");
$diff = $end_time - $start_time;
echo date('H:i:s', $diff);
Your $diff variable is not a timestamp, it's a duration/interval. The date() function is intended to format timestamps, and won't properly handle intervals like you're expecting.
Instead, try using the DateTime class to read your timestamps, and turn the difference between them into a DateInterval using DateTime::diff(). You can then use DateInterval::format to get the output you want.
Something like this should work:
$start_time = new DateTime("0:17:14");
$end_time = new DateTime("0:17:34");
$diff = $end_time->diff($start_time);
echo $diff->format('%H:%I:%S');

Get the current date and time in PHP with '2016-07-04 00:00:00.000' format [duplicate]

This question already has answers here:
How to get GMT date in yyyy-mm-dd hh:mm:ss in PHP
(6 answers)
Closed 6 years ago.
I am using mssql db with php. Which PHP function can return the current datetime. (i.e.) I want the current date and time to be saved in the following format say for example,
2016-07-04 11:10:05.000
Thanks!
Use date->format http://php.net/manual/it/function.date.php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s.u');
12-hour format
date("Y-m-d h:i:s.u")
24-hour format
date("Y-m-d H:i:s.u")
With "date" you can format the output and with "time" you get the current unix time.
Example
echo date("Y-m-d H:i:s.u", time());
Documentation: http://php.net/manual/it/function.date.php

Add one month to gmdate using current format [duplicate]

This question already has answers here:
Add a 1-hour expiration to the current time/date
(5 answers)
Closed 7 years ago.
Hello I have the following php code:
$date = gmdate("Y-m-d\TH:i:s\Z"); /*Format: '2015-05-24T00:00:00Z'*/
How would I simply add 1 month to the above date while keeping the same format?
I tried a few things that lead to nowhere, I am sure there should be a way of doing it that I can't seem to find.
Any help is much appreciated!
Thanks,
Al
try below:
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime("+1 month"));
Try with -
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime('+ 1 MONTH'));
echo $date;
It will add one month to the date.

php: how to add minutes to timestamp [duplicate]

This question already has answers here:
PHP: add seconds to a date
(9 answers)
Closed 8 years ago.
I am trying to figure out how to easily add minutes to a timestamp in php.
I can do this easily in SQL such as select now() + interval '60 minutes' or using the DATEADD function, however I am not sure how to easily do this in PHP.
I have a date variable that I call as:
$date = date("Y-m-d H:i:s");
And now I just need to add or subtract time from this.
Can anybody help?
Thanks!
You could use strtotime() like this:
$date = date('Y-m-d H:i:s', strtotime('now +60 minutes'));
Which would give you the date 60 minutes in the future formatted the way you'd like.
If you have the timestamp is like this
$tmt += 2 * 60 * 1000;
$date= date("Y-m-d H:i:s", $tmt);
Try this

Get date from the three and a half months ago

I know I can do
$threemonthsago = date("Y-m-d", strtotime("-3 month"));
echo "$threemonthsago";
However I want to get 3.5 months ago not three months ago. So I tried to do
$threemonthsago = date("Y-m-d", strtotime("-3.5 month"));
echo "$threemonthsago";
However it does not seem to give me the correct date its giving me like September something which it should not be since its currently April.
The decimal throws off strtotime() as that is not a valid format it recognizes. The real issue you have is what exactly is half of a month? If you traverse February it gets really dicey.
This is somewhat easier to do using DateTime() and DateInterval() if you specify exactly what half of a month is:
$date = new DateTime();
$new_date = $date->sub(new DateInterval('P3M15D'));
echo $new_date->format('Y-m-d');
Demo
echo(strtotime("-105 days"));

Categories