82

I have got two arrows set up, click for next day, next two days, soon and previous day, two days ago, soon. the code seem not working? as it only get one next and previous day.

<a href="home.php?date=<?= date('Y-m-d', strtotime('-1 day', strtotime($date))) ?>" class="prev_day" title="Previous Day" ></a> 
<a href="home.php?date=<?= date('Y-m-d', strtotime('+1 day', strtotime($date))) ?>" class="next_day" title="Next Day" ></a>

is there a way if i click the next button, the date will continously change for the next day. for a moment it will only get one day ahead

Shoaib Quraishi
  • 232
  • 2
  • 17
tonoslfx
  • 3,422
  • 15
  • 65
  • 107
  • 1
    Exactly what are you expecting? Your question is not clear. – Nick May 04 '11 at 12:45
  • 1
    Your code creates one link for the next day and one for previous day. What is your question? – Gordon May 04 '11 at 12:46
  • Is the value on $date changed on each page? – Emil Vikström May 04 '11 at 12:46
  • sorry for unclear question! if, you click next day button, it will show the 'next day date', then if you click again, it will show the 'next two days date'... – tonoslfx May 04 '11 at 12:48
  • @Gordon, `strtotime` only takes one perameter – Alan Whitelaw May 04 '11 at 12:48
  • @Alan Nope: `int strtotime ( string $time [, int $now ] )` See http://de2.php.net/manual/en/function.strtotime.php – Gordon May 04 '11 at 12:51
  • @boyee what happens when you use `$_GET['date']` instead of `$date`? – Gordon May 04 '11 at 12:59
  • @Gordon: `$date is for todays date`. `$_GET['date']` use to get report base on the date. let say, if u click next, it will get for tmr date `2011-05-04`, if click again `2011-05-05`, again `2011-05-06` soon.... – tonoslfx May 04 '11 at 13:02
  • @boyee that doesnt answer my question. What happens when you use $_GET['date'] instead of $date? The second argument to strtotime is the timestamp from which relative dates will be calculated so when you pass in today's date it will of course only do tomorrow and yesterday. You have to pass in the date from the query string to have it give the date after the next day or the day before yesterday. If $date is always todays date it cannot work. – Gordon May 04 '11 at 13:05
  • @Gordon. Apologies I have always used one parameter, `strtotime('2011-05-03 -1 day')` Everyday is a school day! – Alan Whitelaw May 04 '11 at 13:07
  • @Gordon: thanks i know what u mean :) so i just change the $date to $_GET['date']. – tonoslfx May 04 '11 at 13:08
  • ...and what about a NICE&&MODERN way? : `(new DateTime('now+1day'))->format('Y-m-d')` ...that is tomorrow :) You can guess how would it look for yesterday... :) – jave.web Apr 13 '16 at 01:05

12 Answers12

250
date('Y-m-d', strtotime('+1 day', strtotime($date)))

Should read

date('Y-m-d', strtotime(' +1 day'))

Update to answer question asked in comment about continuously changing the date.

<?php
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$next_date = date('Y-m-d', strtotime($date .' +1 day'));
?>

<a href="?date=<?=$prev_date;?>">Previous</a>
<a href="?date=<?=$next_date;?>">Next</a>

This will increase and decrease the date by one from the date you are on at the time.

DarkSide
  • 3,670
  • 1
  • 26
  • 34
Alan Whitelaw
  • 16,171
  • 9
  • 34
  • 51
  • 1
    thanks. is there a way if i click the next button, the date will continously change for the next day, if u know what i mean. for a moment it will only get 1 day ahead – tonoslfx May 04 '11 at 12:52
  • $date=date('Y-m-d'); echo date($date,strtotime('-1 days')); its not give previous date. IT display only current date. How to solve this? –  Apr 17 '14 at 12:11
  • 1
    date('Y-m-d', strtotime('+1 day', strtotime($date))) is fine. That allows you to feed in the date you are calculating from, rather than the current date. – Keith Grey Oct 24 '16 at 18:53
8

Simply use this

echo date('Y-m-d',strtotime("yesterday"));
echo date('Y-m-d',strtotime("tomorrow"));
Sarwar Hasan
  • 1,561
  • 2
  • 17
  • 25
7

Requirement: PHP 5 >= 5.2.0

You should make use of the DateTime and DateInterval classes in Php, and things will turn to be very easy and readable.

Example: Lets get the previous day.

// always make sure to have set your default timezone
date_default_timezone_set('Europe/Berlin');

// create DateTime instance, holding the current datetime
$datetime = new DateTime();

// create one day interval
$interval = new DateInterval('P1D');

// modify the DateTime instance
$datetime->sub($interval);

// display the result, or print_r($datetime); for more insight 
echo $datetime->format('Y-m-d');


/** 
* TIP:
* if you dont want to change the default timezone, use
* use the DateTimeZone class instead.
*
* $myTimezone = new DateTimeZone('Europe/Berlin');
* $datetime->setTimezone($myTimezone); 
*
* or just include it inside the constructor 
* in this form new DateTime("now",   $myTimezone);
*/

References: Modern PHP, New Features and Good Practices By Josh Lockhart

Erald Karakashi
  • 860
  • 8
  • 5
  • 3
    DateInterval is not actually intended for this purpose :) , you can just do `(new DateTime('now+1day'))->format('Y-m-d')` – jave.web Apr 13 '16 at 01:03
6

Use

$time = time();

For previous day -

date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)- 1 ,date("Y", $time)));

For 2 days ago

date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time) -2 ,date("Y", $time)));

For Next day -

date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)+ 1 ,date("Y", $time)));

For next 2 days

date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time) +2 ,date("Y", $time)));
uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42
  • 1
    Are you sure this works in edge cases? For example if the current day is 31th. What will happen when you try to get +1? – Pavel Nikolov Jan 09 '13 at 12:55
  • @PavelNikolov Look at the [mktime() example #2](http://php.net/manual/en/function.mktime.php). "Wrong inputs" like "32" are allowed. – mgutt Apr 03 '15 at 12:15
4

it is enough to call it this way:

<a href="home.php?date=<?= date('Y-m-d', strtotime('-1 day')) ?>" class="prev_day" title="Previous Day" ></a>
<a href="home.php?date=<?= date('Y-m-d', strtotime('+1 day')) ?>" class="next_day" title="Next Day" ></a>

Also see the documentation.

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
shadyyx
  • 15,825
  • 6
  • 60
  • 95
3

You could use 'now' as string to get today's/tomorrow's/yesterday's date:

$previousDay = date('Y-m-d', strtotime('now - 1day'));
$toDay       = date('Y-m-d', strtotime('now'));
$nextDay     = date('Y-m-d', strtotime('now + 1day'));
Alex Joe
  • 369
  • 3
  • 8
3
strtotime('-1 day', strtotime($date))

This returns the number of difference in seconds of the given date and the $date.so you are getting wrong result .

Suppose $date is todays date and -1 day means it returns -86400 as the difference and the when you try using date you will get 1969-12-31 Unix timestamp start date.

hakre
  • 193,403
  • 52
  • 435
  • 836
Prajwal GN
  • 55
  • 1
  • 4
1

always make sure to have set your default timezone

date_default_timezone_set('Europe/Berlin');

create DateTime instance, holding the current datetime

$datetime = new DateTime();

create one day interval

$interval = new DateInterval('P1D');

modify the DateTime instance

$datetime->sub($interval);

display the result, or print_r($datetime); for more insight

echo $datetime->format('Y-m-d');

TIP:

If you don't want to change the default timezone, use the DateTimeZone class instead.

$myTimezone = new DateTimeZone('Europe/Berlin');
$datetime->setTimezone($myTimezone); 

or just include it inside the constructor in this form new DateTime("now", $myTimezone);

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

Php script -1****its to Next Date

<?php

$currentdate=date('Y-m-d');


$date_arr=explode('-',$currentdate);


$next_date=
Date("Y-m-d",mktime(0,0,0,$date_arr[1],$date_arr[2]+1,$date_arr[0]));



echo $next_date;
?>**

**Php script -1****its to Next year**


<?php

$currentdate=date('Y-m-d');


$date_arr=explode('-',$currentdate);


$next_date=
Date("Y-m-d",mktime(0,0,0,$date_arr[1],$date_arr[2],$date_arr[0]+1));



echo $next_date;
?>
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
0

just in case if you want next day or previous day from today's date

date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));

just change the "-1" to the "+1" regards, Yosafat

Yosafat Ksatria
  • 133
  • 1
  • 1
  • 14
0

Very easy with the dateTime() object, too.

$tomorrow = new DateTime('tomorrow');
echo $tomorrow->format("Y-m-d"); // Tomorrow's date

$yesterday = new DateTime('yesterday');
echo $yesterday->format("Y-m-d"); // Yesterday's date
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
0
$Day_seconds = 86400;

$next_day = $day_timestamp + $Day_seconds;
$prev_day = $day_timestamp - $Day_seconds;