Adding days/weeks to formatted date - php

I have a date formatted like this: Ymd
I can't seem to find a way to be able to add a number of weeks to this date, I have tried the following:
$quote_start_date = $job['quote_authorised'];
$newdate = date($quote_start_date, strtotime('+5 weeks'));
However the new date is the same, what is the easiest way to add weeks to a date formatted like this?

The 2nd parameter to date takes seconds since epoch. Just add 5 weeks in seconds to the time, ie:
$newdate = date($format, strtotime($quote_start_date) + (5 * 7 * 24 * 60 * 60));
Or just use the constant value "3024000"
$newdate = date($format, strtotime($quote_start_date) + 3024000);

The first parameter of date expects a format for your outputted date string. I think you're looking for the following:
$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime('+5 weeks', strtotime($quote_start_date)));
The more efficient approach would be to use a fixed value for the number of seconds in a week and not rely on PHP parsing an additional strtotime function:
$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime($quote_start_date) + 3024000);

Its a demo :
$date = new Date();
$nextDate = new Date($date.setTime( $date.getTime() + 1 * 86400000 ));
// here 1 is number of day .

Related

How to get next time occurrence from now, submitted in php?

I have an input file with type time. what I want to do is to get time from the moment now that is if time right now is 2019-11-26 23:50:00 and the value of input field if 22:30:00, I should get a date 2019-11-27 22:30:00.
How can achieve this in PHP? in short, get the datetime stamp for the next occurrence of 22:30:00 which is 2019-11-27 22:30:00 as per the given example.
answer found but can we optimize the code more ?
$a = explode(':', date('H:i:s', strtotime($validateData['time'])));
$str = '+'.$a[0].' hours '.$a[1].' minutes '.$a[2].' seconds';
$trigger_at = date(date('Y-m-d H:i:s', strtotime( $str, strtotime($validateData['date']))));
return $trigger_at;
This is simpler and a lot more readable
$time = "21:30:00"; // Time from input
$today = date("Y-m-d $time");
$tomorrow = date("Y-m-d H:i:s", strtotime($today)+86400);
$date = strtotime($today) < strtotime("now") ? $tomorrow : $today;
Explanation: We take timestamp at specified hour for today and tomorrow, if today timestamp has been passed, we use tomorrow timestamp. Simple. :)
All you are doing is appending (concatenating) a string onto another string.
$time = "22:30:00"; // This is the time you have
$date = date("Y-m-d"); // Right now in yyyy-mm-dd format.
$newdatetime = $date.' '.$time;
That will give you the current date with the supplied time appended to it. You can convert that back into a timestamp using:
$timestamp = strtotime($newdatetime);
The answer below is based on the original question in which the time was assumed to be an offset from now. It is left here simply to avoid deleting a lot of code.
The function strtotime is easy to use for that. However, it doesn't accept HH:MM:SS format. So, you have to alter the string. I would do it like:
$time = "22:30:00"; // This is the time you have
$a = explode(':', $time);
$str = '+'.$a[0].' hours '.$a[1].' minutes '.$a[2].' seconds'; // This breaks it into separate numbers with labels.
$date = date("Y-m-d h:i:s", strtotime($str)); // The adjusted date
You can change the format of the output as you like by changing the first string used in the date function.

How to get date when given a date and days from that date? php

I have to get a date in the future.
I have a date in the past and I need to add "X" days and get the resulting date.
Here is an example of what I want to do
2016-02-28 09:07:22 + 62 days = NewDate
I am using PHP
You have to use strtotime() function to convert the date string into seconds, then add the days, and then convert back with date() function if you want. The code will look like this:
$dateString = '2016-02-28 09:07:22';
$time = strtotime($dateString); //convert to seconds
$timeNew = $time + 62 * 24 * 60 * 60; //add 62 days in seconds
$dateNew = date("Y-m-d H:i:s", $timeNew); //convert back
If you are using the \DateTime() object, you can do something like:
$past = new \DateTime('2016-02-28 09:07:22');
$interval = new \DateInterval('P64D'); // 64 days
$future = $past->add($interval);

retrive date after 1.3 year from the given date in php

I am working on date and I am stuck at a point. How can I get the date after 1.2 or after 1.5 year from the given date?
My code is as follows:
$date = date("Y-m-d", strtotime($from_date . ' +'.$valid_duration.' '.$day) );
where $valid duration can be number as 1, 2, 1.2, etc. and $day is year, months, days.
To get the future date try this:
$StartingDate = date('Y-m-d'); // todays date as a timestamp
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StartingDate)) . " + 1 year 2 months 5 days"));
Hope this helps.
Not sure if you can define years/months/days with a decimal point.
But regardless, have you considered just using the timestamp?
86400 seconds in a day, so say you wanted to get date from one year in the future, you could use something like:
$thetime = time() + (365 * 86400);
$date = date("Y-m-d", $thetime);

What's the best way of adding a time interval in PHP?

I want to add time to an existing date. I have 2 string variables:
$date = "2013-01-05 10:55:15";
$interval = "50:25:10";
I want to calculate the final date "2013-01-07 13:20:25". The hours in time can be bigger than 23, meaning that this interval can be greater than a whole day.
What's the best way to do this ?
Use DateTime API:
$date = new DateTime("2013-01-05 10:55:15");
$date->add(new DateInterval("PT50H25M10S"));
then you can convert it back to string with the same date format you would use with date() function, if you want to:
$string = $date->format("Y-m-d H:i:s");
For more information about the DateInterval definition, visit this page:
DateInterval
The format starts with the letter P, for "period." Each duration
period is represented by an integer value followed by a period
designator. If the duration contains time elements, that portion of
the specification is preceded by the letter T.
Here are some simple examples. Two days is P2D. Two seconds is PT2S.
Six years and five minutes is P6YT5M.
so in this case PT50H25M10S means 50 hours, 25 minutes, and 10 seconds
Note that DateInterval is available only since PHP 5.3, if you have to use lower version, you could use something like this:
$time = strtotime("2013-01-05 10:55:15");
$time += 55*60*60 + 25*60 + 10;
$newDate = date("Y-m-d H:i:s");
This is a little tricky.
Normally what you would do here if it was a static period, or was a single period type, is something along the lines of:
$date = "2013-01-05 10:55:15";
$time = new DateTime($date);
$time->add(new DateInterval('PT5M'));
This would add 5 minutes to the datetime. However I doubt you can pass the whole interval in. So what you'll probably have to do is split the interval by : and then add each part of the interval (I assume it is dynamic?) to the date separately. So first hours, then minutes, then seconds
For more on this, see here: http://www.php.net/manual/en/datetime.add.php
You could first explode the interval and then get the hours, minutes, seconds, and then use DateTime's add() to add the interval, like so:
$interval = '50:25:10';
$datestring = '2013-01-05 10:55:15';
list($hours, $minutes, $seconds) = explode(':', $interval);
$date = new DateTime($datestring);
$date->add(new DateInterval('PT'.$hours.'H'.$minutes.'M'.$seconds.'S'));
echo $date->format('Y-m-d H:i:s');
Demo!
Use a DateInterval and DateTime->add():
$date = new DateTime("2013-01-05 10:55:15");
$date->add(new DateInterval("PT50H25M10S"));
In case anyone needs based on answers above, I made my function like this:
public function addTime($date, $time) {
$atime = explode(":", $time);
$_date = date_create_from_format("Y-m-d H:i:s", $date);
$_date->add(new DateInterval("PT" . intval($atime[0])."H".intval($atime[1])."M".intval($atime[2])."S"));
return $_date->format("Y-m-d H:i:s");
}
hope it helps someone

get current date and date after two months in php

this is a very lame question but i m not able to find this one.
How to get today's date and date after two months..
format is month-date-year (numerical.)
You can use the strtotime() function :
$today = time();
$twoMonthsLater = strtotime("+2 months", $today);
// If what you really want is exactly 60 days later, then
$sixtyDaysLater = strtotime("+60 days", $today);
// ...or 8 weeks later :
$eightWeeksLater = strtotime("+8 weeks", $today);
In any case, the resulting new timestamps can then be converted to month-date-year :
echo 'Today is : ' . date('m-d-Y', $today);
echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);
** UPDATE **
From the PHP manual
Note: Please keep in mind that these functions are dependent on the locale settings of your server. Make sure to take daylight saving time (use e.g. $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions.
Just thought I should mention it...
There are a couple of ways you could go about it - the first one would be to do something like this:
echo $currentdate = date("Y-m-d H:i:s",time());
echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);
Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months.
Another way to do it, which is my preferred way and how I would do it, is this:
$after60days = strtotime("+60 days");
The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function.
Of course, if you need to output a date in a format that easy to read for humans, you can do something like this:
echo date('Y-m-d H:i:s',$after60days);
Today:
date('m-d-Y', time());
Two months from now:
date('m-d-Y', time() + (86400 * 60));

Categories