strtotime PHP for date numerical value + 1 week - php

I have a day value of 1 through 7 where 1 is Monday and 7 is Sunday.
I need to get a strtotime value of the appropriate day NEXT week.
For example:Today is Tuesday 13th November.
My day value is "2" so strtotime should return the appropriate value for Tuesday 20th November.
My day value is "1" so strtotime should return the appropriate value for Monday 19th November.
My day value is "5" so strtotime should return the appropriate value for Friday 23rd November.
I'm hoping this can be done with just a few built in PHP functions (strtotime(+1week+something))?
If not I will attempt to code some comparison checks!
Thanks!

function dayNextWeek($num) {
return date('Y-m-d', strtotime("+1 week +" . ($num - date('w')) . "days"));
}
And to test:
foreach (range(1, 7) as $i) {
echo $i . ' ' . dayNextWeek($i) . "\n";
}
OUTPUT
1 2012-11-19
2 2012-11-20
3 2012-11-21
4 2012-11-22
5 2012-11-23
6 2012-11-24
7 2012-11-25

Not tested extensively:
$givenDayNumber = 1; // Your value here
$date = new DateTime(); // Today (reference point)
$currentDayOfWeek = date('N'); // This is 1 = Monday, see php date manual for more options
$diff = $givenDayNumber- $currentDayOfWeek; // Difference between given number and todays number
$date->modify('+ '.$diff.' day'); // Subtract difference
$date->modify('+ 1 week'); // Add a week
echo $date->format('d/m/Y'); // Output
Codepad here for fiddling: http://codepad.org/7SylbJOX

function getNext($dow)
{
$now = intval(date('N'));
$toAdd = $dow - $now;
if($toAdd<=0)
{
$toAdd += 7;
}
return date('r', strtotime('+'.$toAdd.'day'));
}

Hopefully, This should work for you.
<?php
$array = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
$your_value = 2;
$str = 'next ' . $array[$your_value -1];
echo date('D d M Y', strtotime($str));
?>
http://codepad.org/OIrzRxpl

Try:
$day_nxt_week = date('N', strtotime(date('Ymd') . '+1 week'));
print_r($day_nxt_week);
Demo

Related

1st I converted date to week number ( 01 to 53 ) in PHP and now i don't know how to get week number to first_date of week

Here a simple code of
date to week_number of year and now. How can I get start_date and end_date of week_number
$date_string = "2012-12-30";
echo "Weeknummer: " . date("W", strtotime($date_string));
There is many ways to do it, here is one.
I use "N" of date to determine what day the selected day is and use strtotimes way of understanding simple text to find previous or next Monday/Sunday.
$date_string = "2018-01-27";
$w =date("W", strtotime($date_string));
$N =date("N", strtotime($date_string));
If($N == 1){ // if monday
$monday = $date_string;
$sunday = date("Y-m-d", strtotime("next Sunday $date_string"));
}Elseif($N ==7){ // if sunday
$monday = date("Y-m-d", strtotime("previous Monday $date_string"));
$sunday = $date_string;
}Else{// any other weekday
$monday = date("Y-m-d", strtotime("previous Monday $date_string"));
$sunday = date("Y-m-d", strtotime("next Sunday $date_string"));
}
echo "Weeknummer: $w.\nMonday: $monday.\nSunday: $sunday.";
Output:
Weeknummer: 52.
Monday: 2012-12-24.
Sunday: 2012-12-30.
https://3v4l.org/UDoqF
You can use this:
<?php
$date_string = "2012-12-30";
$weekNumber = date("W", strtotime($date_string));
echo "Weeknummer: ".$weekNumber;
echo '</br>';
// get the year
$year = date("Y", strtotime($date_string));
// set the date string for the week number
$dateWeek = $year.'-W'.$weekNumber;
// increase the weekNumber to the next
$weekNumber = intval($weekNumber);
$weekNumber += 1;
// if it is lower than 10 add preceeding 0
if($weekNumber < 10) $weekNumber = '0'.$weekNumber;
// set the date string for the next week number
$dateWeekNext = $year.'-W'.$weekNumber;
echo '</br>';
// get the first day of the week
echo date('Y-m-d', strtotime($dateWeek));
echo '</br>';
// get the day before the first day of the next week
echo date('Y-m-d', strtotime($dateWeekNext . ' -1 day'));
?>
This outputs:
Weeknummer: 52
2012-12-24
2012-12-30

php how to store monday to friday of the next 6 months begin from tomorrow in an array

I am making a page to let the clients choose a date for an appointment, so I need to build a list of the dates like this :
always begin from tomorrow, end by 6 months
always from Mondy to Saturday, no Sunday
the day of the week need to be in chinese, like "Monday" is "星期一", but the timezone is in France
Here is the php
date_default_timezone_set('Europe/Paris');
$tomorrow = date("Y年m月d日 l", time() + 86400);
$end = date("Y年m月d日 l", time() + 86400 * 7); // just 7 days for a try
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($tomorrow, $interval, $end);
foreach ($daterange as $date) {
echo $date . '<br/>';
}
This code is not working.
I need to build an array, which store all the dates of next 6 months, begin from tomorrow, without Sunday, the days need to be in chinese and the timezone needs to be in Europe, is that possible?
I think strtotime and array_push is what you are looking for. Try this:
$curDate = date('Y-m-d', strtotime('+1 day'));
$endDate = date('Y-m-d', strtotime('+6 months +1 day'));
$myArr = array();
while ($endDate >= $curDate) {
if (date('w', strtotime($curDate)) !== '0') array_push($myArr, $curDate);
$curDate = date('Y-m-d', strtotime($curDate . " +1 days"));
}
var_dump($myArr);
For the Options, create an array from sunday to saturday.
$weekdays = array('Sunday', ..., 'Saturday');
echo date('Y/m/d', strtotime($curDate)) . ' ' . $weekdays[date('w', strtotime($curDate))];

Date plus one month php [duplicate]

This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 6 years ago.
I have a small problem with dates in PHP.
When I made 31 + 1 month of January
with this code
$newDate = date('Y-m-d', strtotime('31-01-2016'.' + 1 month'));
echo $newDate;
it gives me 2 March but I need given me 29 February,
I need to add 1 month and is not 30days.
ditto for all dates:
for example
01 january + 1 month => 1 february
29 january + 1 month => 29 february
30 january + 1 month => 29 february
31 january + 1 month => 29 february
Thank for your help
I think you are looking for this type of dates.
<?php
$date = date('2016-01-31');
$currentMonth = date("m",strtotime($date));
$nextMonth = date("m",strtotime($date."+1 month"));
if($currentMonth==$nextMonth-1 && (date("j",strtotime($date)) != date("t",strtotime($date)))){
$nextDate = date('Y-m-d',strtotime($date." +1 month"));
}else{
$nextDate = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
}
echo "Next date would be : $nextDate";
?>
Check live demo : https://eval.in/610034
If date is 31-01-2016 then next date would be 29-02-2016
If date is 25-01-2016 then next date would be 25-02-2016
Simply try:
$date = new DateTime('2016-01-31');
$date->modify('last day of next month');
This of course only counts if you always go from the end of one moth to the end of the next one.
try this,
$date = "2016-01-29";
$date = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
echo $date;
https://3v4l.org/Y9PpV
How about something like this:
date_default_timezone_set('UTC');
$current_month = (int) date('m');
$year = date('y');
$newDate = date('Y-m-d', strtotime('31-1-2016'.' + 1 month'));
if($current_month == 12)
{
$new_month=0;
$year++;
}
$d = new DateTime( $year.'-'.($current_month+1).'-01' );
echo $d->format( 'Y-m-t' )."\n";
Change $current_month / $year based on your needs......

PHP find closest specific day to date

I'm writing a script that gets some statistics about my website for a date this year, but I also want it to get me the data for the nearest corresponding day last year so I can compare them.
For example if I were to get the data for "Wednesday 14th Dec 2011", I'd want to also get the data for "Wednesday 15th Dec 2010".
I'm having a bit of trouble thinking of how to get the correct date from this year's date. I'd prefer to be able to pass the data into a function, something like:
// $date as a unix timestamp
// $day (0 for Sunday through 6 for Saturday)
function getClosestDay($date,$day=0) {
}
So I would be passing in a unix timestamp of last year's date, and also the day I'm looking to find. I'd expect it to return a unix timestamp of the correct day.
But I'm not sure where to even start with the function.
I'm not looking for someone to write the function for me, but if anyone has any ideas on where to start (even a nudge in the right direction) then that would be great!
I believe this would do. First we get the unix time of the same day last year
$newDate = '14th Dec 2011';
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
Now we find the difference in week day. In this example, it'll be -1
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
And then we extract/add that difference to the date
$oldDate = strtotime("$dayDiff days",$oldDate);
And output it
print date('r',$oldDate)."\n";
print date('r',$newDate)."\n";
The above should yield
Wed, 15 Dec 2010 00:00:00 +0100
Wed, 14 Dec 2011 00:00:00 +0100
Here is what I came up with:
function getClosestDate($date, $day = 0, $year = -1) {
$cts = strtotime($date);
$ts = strtotime("{$year} YEAR", $cts);
$days = array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
);
$day = $days[$day];
$prev = strtotime("PREVIOUS {$day}", $ts);
$next = strtotime("NEXT {$day}", $ts);
$prev_gap = $ts - $prev;
$next_gap = $next - $ts;
return $prev_gap < $next_gap ? $prev : $next;
}
echo date('Y-m-d', getClosestDate('2011-12-12', 1));
// prints 2010-12-13 (closest Monday to 2010-12-12)
echo date('Y-m-d', getClosestDate('2011-12-12', 4));
// prints 2010-12-09 (closest Thursday to 2010-12-12)
And by the way (and fortunately), December 14th 2011 is not a Monday. :)
I got it, here it goes:
function getClosestDay($date) {
$w = date("w", strtotime("-1 year", $date));
$week = date("w", $date);
$days = ($week - $w);
return date("Y-m-d l", strtotime($days . " days -1 year", $date));
}
echo getClosestDay(mktime(0, 0, 0, 12, 14, 2011));
Try this
function getClosestDay($date,$day=0) {
$days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
return strtotime('last ' . $days[$day], $date);
}
Here is a less elegant but tested function :
function getClosestDay($time,$dayOfTheWeek) {
$last_year=strtotime("last year",$time);
$next=strtotime("next $dayOfTheWeek",$last_year);
$last=strtotime("previous $dayOfTheWeek",$last_year);
$most_near = (min($next-$last_year,$last_year-$last) == ($next-$last_year)) ? $next : $last;
return $most_near;
}
Have you tried strtotime?
strtotime('-1 year',time());

php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

Categories