PHP, Get tomorrows date from date
Asked Answered
S

10

110

I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23.

How is this possible with PHP?

Steen answered 22/1, 2013 at 14:10 Comment(0)
C
233

Use DateTime

$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');

Or:

$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');

Or:

$datetime = new DateTime('2013-01-22');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');

Or in PHP 5.4+:

echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
                                 ->format('Y-m-d H:i:s');
Claptrap answered 22/1, 2013 at 14:12 Comment(1)
This uses current, I need to pass in a given date.Steen
D
110
 $tomorrow = date("Y-m-d", strtotime('tomorrow'));

or

  $tomorrow = date("Y-m-d", strtotime("+1 day"));

Help Link: STRTOTIME()

Dicarlo answered 15/12, 2015 at 9:47 Comment(2)
I used this one and of all answers is the shortest and simplest version, thanks !Aleydis
This is cool and will also give you yesterday: -1 dayHysterogenic
C
17

Since you tagged this with , you can use it with the +1 day modifier like so:

$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));

That said, it's a much better solution to use DateTime.

Chloral answered 22/1, 2013 at 14:14 Comment(0)
L
14
<? php 

//1 Day = 24*60*60 = 86400

echo date("d-m-Y", time()+86400); 

?>
Logger answered 24/10, 2013 at 10:35 Comment(2)
Note that this will fail in edge cases (daylight savings time).Sudderth
This answer is wrong. I have a cronjob use this in a loop, and the system got died.Hedjaz
B
8

echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));

Bratcher answered 22/6, 2017 at 5:12 Comment(0)
B
5

Use DateTime:

To get tomorrow from now :

$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;

Results : 28/06/2017 08.13.20

To get tomorrow from a date :

$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;

Results : 11/06/2017 08.16.35

Hope it helps!

Bussard answered 27/6, 2017 at 1:18 Comment(0)
H
1
/**
 * get tomorrow's date in the format requested, default to Y-m-d for MySQL (e.g. 2013-01-04)
 *
 * @param string
 *
 * @return string
 */
public static function getTomorrowsDate($format = 'Y-m-d')
{
    $date = new DateTime();
    $date->add(DateInterval::createFromDateString('tomorrow'));

    return $date->format($format);
}
Hege answered 9/6, 2015 at 16:26 Comment(0)
C
1

By strange it can seem it works perfectly fine: date_create( '2016-02-01 + 1 day' );

echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );

Should do it

Cortez answered 2/2, 2016 at 12:51 Comment(0)
H
-1

here's working function

function plus_one_day($date){
 $date2 = formatDate4db($date);
 $date1 = str_replace('-', '/', $date2);
 $tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
 return $tomorrow; }
Hamlani answered 31/1, 2017 at 8:53 Comment(2)
what does the formatDate4db function do? Why replace the dashes with slashes in the string?Emmuela
formatDate4db function pass the date and it make a format like database mysql format , and str_replace function , i have date format 2019/12/10 and replace with -Hamlani
Y
-4
$date = '2013-01-22';
$time = strtotime($date) + 86400;
echo date('Y-m-d', $time);

Where 86400 is the # of seconds in a day.

Yazbak answered 22/1, 2013 at 14:14 Comment(5)
No, one day doesn't always have 86400 seconds.Greta
I don't get the problem. Please explain @GretaSalesman
@Salesman days that make the switch to or from daylight saving time do not have 86400 secondsSosanna
I thought of that, DST is initiated at two o'clock and should therefore never result in a different date?Salesman
The problem isn't daylight savings. Times in a database really should be stored as UTC which has no daylight saving time. If you're storing your dates in UTC this will work fine but is a bit obtuse.Emmuela

© 2022 - 2024 — McMap. All rights reserved.