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);
Related
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 .
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.
Lets take any month be of 30 days, then for the date 25/02/2015 when we add 10 days it becomes 5/03/2015 but there are months which are of 29 and 31 days as well.If a month be of 31 then adding 10 days to 25/04/2015 would be 04/05/2015 not 05/05/2015.Does date functionality of MySQL ,PHP and Carbon function of laravel produce correct date after addition or substraction by detecting total days of particular months strictly as per calendar or just assumes every month to be of 30 days?What are the best tools for correct date manipulation?
PHP, yes.
<?php
$date=date_create("2015-02-25");
date_add($date, date_interval_create_from_date_string("10 days"));
echo date_format($date,"Y-m-d");
// echos 2015-03-07
date_add($date, date_interval_create_from_date_string("-10 days"));
echo date_format($date,"Y-m-d");
// echos 2015-02-25
MySQL, yes.
DATE_ADD(SomeDate, INTERVAL 10 DAY)
DATE_SUB(SomeDate, INTERVAL 10 DAY)
You can use like this to get current date,previous date and next date
<?php
$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'));?>
I have a fixed date from MySql
startDate = 07/03/2011
I wanted to add 60 days on top this date to have an endDate.
$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;
From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.
Help? Thanks in advance!
In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:
SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;
-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;
Relevant documentation here...
You could reformat the results of strtotime()
$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));
Demo: http://codepad.org/9rWnoeQb
$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;
86400 seconds in a day, times number of days.. and add it to current time.
$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);
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));