How can I get next week dates? - php

Problem
I'm using the below code to get the next week's date and second week's date. It works fine for first few records but later it starts giving year 1970.
If the start date is 12/01/2013 it shows me coorect result that is:
Next week: 19/01/2013
Second week: 26/01/2013
but in another record where the date is 16/05/2013 it shows the below
Next week: 08/01/1970
Second week: 15/01/1970
Please guide me where I might be going wrong ?
Code
//Date of when game started
$starts_on = '12/01/2013';
//Next week's date from start date
$next_week = strtotime(date("d/m/Y", strtotime($starts_on)) . "+1 week");
$next_week = date('d/m/Y', $next_week);
//Second week's date from start date
$second_week = strtotime(date("d/m/Y", strtotime($starts_on)) . "+2 week");
$second_week = date('d/m/Y', $second_week);
echo $starts_on.", ".$next_week.", ".$second_week;

You are using the wrong format date. Check the note in the strtotime documentation:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
Check the documentation further:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

I recommend you use the DateTime Object, is better to manipulate dates (to add and substract dates from another is very easy with the object DateInterval)
<?php
$date = new DateTime("2013-01-12");
//add one week to date
echo $date->add(new DateInterval('P1W'))->format('Y-m-d');
//add one week to date
echo $date->add(new DateInterval('P1W'))->format('Y-m-d');
?>
Result:
2013-01-19
2013-01-26
References:
http://php.net/manual/es/class.datetime.php
http://php.net/manual/es/class.dateinterval.php

Please try this
$dt = new DateTime();
// create DateTime object with current time
$dt->setISODate($dt->format('o'), $dt->format('W')+1);
// set object to Monday on next week
$periods = new DatePeriod($dt, new DateInterval('P1D'), 6);
// get all 1day periods from Monday to +6 days
$days = iterator_to_array($periods);
// convert DatePeriod object to array
echo "<pre>";
print_r($days);

Related

Sum date using strtotime always returns 01/01/1970

I am trying to add 1 day to a date using strtotime but I canĀ“t get it to work. It always returns 02/01/1970
$date = date ("d/m/Y H:i:s", filemtime($directory));
$newdate = date("d/m/Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
Because strtotime() differentiates between USA date format and Sensible date format by looking at the date seperator all you need to do is us the - seperator if you want to use the sensible date format, like this, in your intermediate date manipulations
$date = date ("d-m-Y H:i:s", filemtime($directory));
$newdate = date("d-m-Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
FROM the manual
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Better work with DateTime():
$date = new DateTime(strtotime(filemtime($directory)));
echo $newdate = $date->format('d/m/Y');
$date->modify('+1 day');
echo $tomorrow = $date->format('d/m/Y');
Output:
20/01/2020
21/01/2020
If your filemtime($directory) returns a string formatted as your date() mask, I mean d/m/Y H:i:s, then you can do next steps:
for example, according to this mask, it looks like:
$s = "02/06/2019 22:23:22";
now you can do strtotime()
$date = date ("d/m/Y H:i:s", strtotime($s));
then transform it to the DateTime object
$st_date = new DateTime($date);
now you can simply modify it as you want
$st_date->modify('+1 days');
to see the result string value use:
$tomorrow = $st_date->format('d/m/Y');
echo 'tomorrow -> '.$tomorrow;
Output:
date->02/06/2019 22:23:22
tomorrow -> 03/06/2019
Demo

Adding x no of days to given date, but it is taking the day number as month and doing the calc. Help me fix the format

I m trying to add 28 days to the given date and echo, however it is taking the given date and considering it as month and adding the days given
Help me fix the format.
my code goes as:
$start = "04/03/2019";
$nextpay1 = date('d/m/Y',strtotime($start . "+28 days"));
echo $nextpay1;
as result it is echoing "01/05/2019", which is not right..
I'll start my answer by quoting the strtotime docs:
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed. If, however, the year is given in a two digit format and
the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD)
dates or DateTime::createFromFormat() when possible.
So, in your case:
$date = DateTime::createFromFormat('d/m/Y', '04/03/2019');
$date->modify('+28 days');
echo $date->format('d/m/Y');
The problem is that $start is being parsed in the default mm/dd/yyyy format. You should use a function to parse it as the format you intend, and then add to that.
$parsed = date_parse_from_format('d/m/Y', $start);
$startdate = mktime(
$parsed['hour'],
$parsed['minute'],
$parsed['second'],
$parsed['month'],
$parsed['day'],
$parsed['year']
);
$nextpay1 = date('d/m/Y', strtotime('+28 days', $startdate));
$date=Date('y:m:d', strtotime("+3 days"));
This will save the date after 3 days

php add x weeks to a date and then find the next given 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.

php first monday after certain date as string

I have a date given as a string. And I would like to get the first monday after this given date, again as a string.
$myDate = "2014-08-24"; // date given in Y-m-d
$nextMonday = ??? // how to get the next monday after $myDate?
This sounds like a question that was solved before, like PHP get next occurrence of Monday from a certain date (with time) or When a date is given how to get the date of Monday of that week in php.
But it actually is a different problem, because I am running into a lot of conversion issues and a little bit confused how to solve this.
you can use this
<?php
$myDate = "2014-08-27";
$next_monday = date('Y-m-d', strtotime("next monday", strtotime($myDate)));
echo $next_monday;
?>
OUTPUT :
2014-09-01
Demo
You can do it using the DateTime and DateTime::modify
$date = new DateTime('2014-08-24');
$date->modify('next monday');
echo $date->format('Y-m-d') . "\n";

PHP: generating an end date with user specified start date and duration

I am trying to create a function that determines the end date of an advert based on the start date and duration parameter passed by a user.
Example:
If user specify start date as 5th June 2013 and duration as 45 days:
$ad_startdate = '2013-06-05';
$ad_duration = 45;
The function should automatically get the end date which should be 20th July 2013:
$ad_end_date = '2013-07-20';
Pls note that to make it easy to generate the end date, I've assigned the variable for months a constant value which will be 30 days. Whether it's february or november or a leap year, every month has a fixed variable value of 30.
I was trying to come up with something around this but just cant figure it out.
$ad_startdate = '2013-06-05';
$ad_duration = 45;
// End date should be 2013-07-20
function getAdvertEndDate ($ad_startdate, $ad_duration){
//Add up start date with duration
$end_date = strtotime($ad_startdate) + $ad_duration;
return $end_date;
}
I have browsed through SO questions just to see if anyone has something around this but the answered ones are so different from mine challenge.
Would be very grateful getting help with this.
function getAdvertEndDate ($ad_startdate, $ad_duration){
return date("Y-m-d", strtotime($ad_startdate) + ($ad_duration * 86400));
}
Use like so:
$endDate = getAdvertEndDate("2013-04-08", 40);
PHP >= 5.3.0 Object oriented style
$date = DateTime::createFromFormat('Y-m-d', '2013-06-05');
$date->add(new DateInterval('P45D'));
echo $date->format('Y-m-d') . "\n";
Or Procedural style
$date = date_create('2013-06-05');
date_add($date, date_interval_create_from_date_string('45 days'));
echo date_format($date, 'Y-m-d');
Result:
2013-07-20
Code:
function getAdvertEndDate ($ad_startdate, $ad_duration){
$date = DateTime::createFromFormat('Y-m-d', $ad_startdate);
$date->add(new DateInterval('P'.$ad_duration.'D'));
return $date->format('Y-m-d');
}
For PHP < 5.3 use strtotime():
function getAdvertEndDate ($ad_startdate, $ad_duration){
//Add up start date with duration
return date('Y-m-d', strtotime($ad_startdate. " + $ad_duration days"));
}
echo getAdvertEndDate('2013-06-05', '45'); // 2013-07-20
http://www.php.net/manual/en/datetime.add.php
Try this code
$date = '2013-06-05';
$date1 = strtotime($date);
$date2 = strtotime('+45 day',$date1);
echo date('Y-m-d', $date2);
The native strtotime() function does this work.
Use this:
$ad_startdate = '2013-06-05';
$ad_duration = 45;
$dateArray = explode('-', $ad_startdate);
$newDate = date('Y-m-d', strtotime('+ ' . $ad_duration . ' days', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]));
If you're using strtotime, you cant use the date format you've specified, as if using - seperators, strtotime() expects the format differently.
From PHP.net
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

Categories