// 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.
I had gone through various stackoverflow solutions and other blogs but still it doesn't fix my problem.
Let's say that the date today is: 2013-12-28 and I want to get the date after 1 month and it is supposed to display 2014-01-28.
$date = date('o-m-d');
$final = date('o-m-d', strtotime("+1 month", $date));
echo $final;
Above is my code. It returns 02/01/1970.
I have also tried the mktime method but still it displays the 1970 output.
What am I doing wrong?
BTW. I am working this on a hosted server.
Thanks ahead. :)
Use DateTime function modify
$date = new DateTime( 'o-m-d' );
echo $date->modify( '+1 month' )->format('o-m-d');
If you want the current date +1 month use:
$final = date('o-m-d', strtotime("+1 month"));
Or with a given date:
$date = date('o-m-d');
$final = date('o-m-d', strtotime($date . " +1 month"));
echo $final;
If you want to use the second parameter of strtotime it has to be a timestamp.
Go the OOP way..
<?php
$date = new DateTime('2013-12-28');
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); //prints 2014-01-28
I'm trying to get php to calculate a date that was one year and one day ago. I have this:
$date = date(strtotime('-366 days'));
$oneyear_oneday = date("Y-m-d H:i:s", $date);
$date = date(strtotime('-1 year'));
$oneyear = date("Y-m-d H:i:s", $date);
However, due to it being a leap year, both $oneyear and $oneyear_oneday provide the same output. Does anyone know how I can calculate this correctly?
ie if it's 3pm on 15th August 2012, I want the output to be 3pm on the 15th August 2011
with PHP5.3,
$date = new DateTime();
$interval = new DateInterval("P1Y");
$newdate = $date->sub($interval);
First, subtract one year. Then, subtract one day from the result:
$date = strtotime('-1 day', strtotime('-1 year'));
$oneyear_oneday = date("Y-m-d H:i:s", $date);
You can try to use mktime()...
Both calculations are correct. But if you want to get the same date, but one year before, you should simply use '-1 year'. The string '-366 days' is only correct in leap years.
$date = strtotime('2010-01-01 -1 year');
echo date('Y-m-d', $date);
The output stream looks like,
2009-01-01
Go this Link for more reference
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