add a day to date, so I can store tomorrow's date in a variable.
$tomorrow = date("Y-m-d")+86400;
I forgot.
date returns a string, whereas you want to be adding 86400 seconds to the timestamp. I think you're looking for this:
$tomorrow = date("Y-m-d", time() + 86400);
I'd encourage you to explore the PHP 5.3 DateTime class. It makes dates and times far easier to work with:
$tomorrow = new DateTime('tomorrow');
// e.g. echo 2010-10-13
echo $tomorrow->format('d-m-Y');
Furthermore, you can use the + 1 day syntax with any date:
$xmasDay = new DateTime('2010-12-24 + 1 day');
echo $xmasDay->format('Y-m-d'); // 2010-12-25
date() returns a string, so adding an integer to it is no good.
First build your tomorrow timestamp, using strtotime to be not only clean but more accurate (see Pekka's comment):
$tomorrow_timestamp = strtotime("+ 1 day");
Then, use it as the second argument for your date call:
$tomorrow_date = date("Y-m-d", $tomorrow_timestamp);
Or, if you're in a super-compact mood, that can all be pushed down into
$tomorrow = date("Y-m-d", strtotime("+ 1 day"));
Nice and obvious:
$tomorrow = strtotime('tomorrow');
You can use the add method datetime class.
Eg, you want to add one day to current date and time.
$today = new DateTime();
$today->add(new DateInterval('P1D'));
Further reference php datetime add
Hope this helps.
I find mktime() most useful for this sort of thing. E.g.:
$tomorrow=date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));
Related
// to simplify $timestamp in this example is the unix timestamp of 2016-04-20
Consider this example:
strtotime('+1 year', $timestamp); // this returns 2017-04-19
How can I make it return 2017-04-20?
Another example:
strtotime('+1 month', $timestamp); // this returns 2016-05-19
How can I make it return 2016-05-20?
Basically, I want to relatively add time that ends up with the same date.
strtotime('+1 day', strtotime('+1 year', $timestamp));
?
$date = date("Y",$timestamp) + 1 //gives you the next year
$date .= "-" . date("m-d",$timestamp) //concantenates on the current month and day
I may be misunderstanding what you're asking but you're probably better of using the DateTime library built into PHP, it's a lot more flexible than the standard date() function.
So you could do:
$d = new DateTime();
$d->modify('+1 year');
echo $d->format('Y-m-d'); // Outputs: 2017-04-20
If you want to create a DateTime object from a specific date you can do so by:
$d = DateTime::createFromFormat('Y-m-d', '2016-01-01');
echo $d->format('Y-m-d'); // Outputs 2016-01-01
I believe that's what you're after, it's much cleaner than date() and easier to read in my personal opinion.
I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.
Assume the date is:
$date = "2011-08-28";
It need to calculate 3 months previous to $date - how can that be done?
$new_timestamp = strtotime('-3 months', strtotime($date));
You can then use the new timestamp with the php date() function to display the new date how you wish, for example:
echo date("Y-m-d",$new_timestamp);
For me this way is much better because the code is more readable.
$datetime = Datetime::createFromFormat('Y-m-d', "2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');
edit: I'm an idiot. You could do the above as follows
$datetime = new Datetime("2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');
edit: As pointed out by calumbrodie, you can use the sub method instead of inverting the interval and adding it to the date object
I was trying to do something similar to the original question. I needed to subtract 1 day from a DateTime object. I know the other solutions work, but here's another way I liked better. I used this function:
function getPreviousDay($dateObject){
$interval = new DateInterval('P1D');
$dateObject->sub($interval);
return $dateObject;
}
$dateObject is a DateTime object that can have any date you wan't, but as I wanted the current date, I wrote:
$dateObject = new DateTime('now');
What my function does, is subtract 1 day from the date it receives, but you can modify it so it subtracts 3 months modifying the DateInterval constructor like this:
$interval = new DateInterval('P3M');
$dateObject->sub($interval);
return $dateObject;
You can find the explanation on the string format used in the DateInterval constructor here
DateInterval constructor documentation
There you'll se that letter 'P' (period) is mandatory, the you use an int to specify period length and then specify the period type, in the case of months 'M'. It looks as if there was an error in the documentation, it says that "M" is used for months and minutes, but it actually works for months. If you need to use minutes, you must specify "PTM", for example "PT3M" for 3 minutes, outside the table it says you must use the "T" identifier for time intervals.
edit:
To give a complete example, you have to use this format for a full date time interval:
$format = 'P1Y2M4DT2H1M40S';
This will represent an interval of 1 year, 2 months, 4 days, 2 hours,1 minute and 40 seconds
Hope this helps someone
<?php
$threemonthsago = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
?>
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>
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
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'));
?>
Previous
Next
This will increase and decrease the date by one from the date you are on at the time.
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
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)));
Simply use this
echo date('Y-m-d',strtotime("yesterday"));
echo date('Y-m-d',strtotime("tomorrow"));
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.
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.
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'));
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);
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;
?>
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
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
I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.
Today is April 09, 2010
7 days from now is April 16,2010
Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.
I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.
If you are using PHP >= 5.2, I strongly suggest you use the new DateTime object, which makes working with dates a lot easier:
<?php
$date = new DateTime("2006-12-12");
$date->modify("+7 day");
echo $date->format("Y-m-d");
?>
Take a look here - http://php.net/manual/en/function.strtotime.php
<?php
// This is what you need for future date from now.
echo date('Y-m-d H:i:s', strtotime("+7 day"));
// This is what you need for future date from specific date.
echo date('Y-m-d H:i:s', strtotime('01/01/2010 +7 day'));
?>
The accepted answer is not wrong but not the best solution:
The DateTime class takes an optional string in the constructor, which can define the same logic as the modify method.
<?php
$date = new DateTime("+7 day");
For example:
<?php
namespace DateTimeExample;
$now = new \DateTime("now");
$inOneWeek = new \DateTime("+7 day");
printf("Now it's the %s", $now->format('Y-m-d'));
printf("In one week it's the %s", $inOneWeek->format('Y-m-d'));
For a list of available relative formats (for the DateTime constructor) take a look at http://php.net/manual/de/datetime.formats.relative.php
If you are using PHP >= 5.3, this could be an option.
<?php
$date = new DateTime( "2006-12-12" );
$date->add( new DateInterval( "P7D" ) );
?>
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$future_date = "April 16,2010";
$seconds = strtotime($future_date) - time();
$days = $seconds /(60 * 60* 24);
echo $days; //Returns "6.0212962962963"
You can use mktime with date. (http://php.net/manual/en/function.date.php)
Date gives you the current date. This is better than simply adding/subtracting to a timestamp since it can take into account daylight savings time.
<?php
# this gets you 7 days earlier than the current date
$lastWeek = mktime(0, 0, 0, date("m") , date("d")-7, date("Y"));
# now pretty-print it out (eg, prints April 2, 2010.)
echo date("F j, Y.", $lastWeek), "\n";
?>