I want to find the timestamp for the most recent 8am encountered. (weekdays only)
For example if its Friday at 3pm I want the timestamp for 8am.
I can do that simply enough, but what about if it is Saturday at 2am.
Also if it is Monday at 6am I want to find Friday at 8am still.
Tried the following:
$timestamp = strtotime(date('Y-m-d') . '08:00:00');
But clearly that only accounts for the current day.
This should do it:
// Choose today if it's past 08:00 and it's not a weekend
if( date( 'G' ) >= 8 && date( 'N' ) <= 5 ) {
$timestamp = strtotime( date( 'Y-m-d' ).' 08:00' );
}
else {
$timestamp = strtotime( 'last weekday 08:00' );
}
Related
I'm trying to get the last weekday of the current month using the code below.
I was expecting this to behave just like all of the other operators like last friday of and output the last weekday of this month. but instead its outputting the last weekday of the previous month.
I know that I can rectify this by using modify( '+1 month' ) but why? Why is it the other operators like last tuesday of work fine but last weekday needs to be bosted by 1 month?
<?php
// outputs the previous months date
$gettoday = date( "Y-m-01" );
$freqdate = date( 'Y-m-d', strtotime( 'last weekday '.$gettoday ) );
echo 'next '.$freqdate;
//get the correct date
$gettoday = new DateTime( $gettoday );
$gettoday->modify( '+1 month' );
$gettoday = date_format( $gettoday, 'Y-m-01' );
$freqdate = date( 'Y-m-d', strtotime( $gettoday . ' last weekday' ) );
echo 'next '.$freqdate;
?>
It appears you had a wrong understanding of what “last” actually means here.
That has nothing to do with the last day or week of the month - it is “last” as in “the closest previous one”.
You can start with something like strtotime('last day of 2020-03') - that would give you 31 Mar 2020, which is a Tuesday.
If you then check what day of the week this is, and it would be either a Saturday or a Sunday - then you can go last friday on that value again, to land on the latest week day of the month.
Edit:
Example for all months of 2020
for($i=1; $i<13; ++$i) {
$year_month = '2020-'.$i;
$last_of_month = strtotime('last day of '.$year_month);
$weekday = date('N', $last_of_month);
if($weekday < 6) {
echo 'last weekday of ', $year_month, ' is ', date('Y-m-d, l', $last_of_month), "<br>\n";
}
else {
$last_weekday_of_month = strtotime('last friday', $last_of_month);
echo 'last day of ', $year_month, ' is ', date('Y-m-d, l', $last_of_month), ' - last weekday of ', $year_month, ' is ', date('Y-m-d, l', $last_weekday_of_month), "<br>\n";
}
}
This will get you the following output:
last weekday of 2020-1 is 2020-01-31, Friday
last day of 2020-2 is 2020-02-29, Saturday - last weekday of 2020-2 is 2020-02-28, Friday
last weekday of 2020-3 is 2020-03-31, Tuesday
last weekday of 2020-4 is 2020-04-30, Thursday
last day of 2020-5 is 2020-05-31, Sunday - last weekday of 2020-5 is 2020-05-29, Friday
last weekday of 2020-6 is 2020-06-30, Tuesday
last weekday of 2020-7 is 2020-07-31, Friday
last weekday of 2020-8 is 2020-08-31, Monday
last weekday of 2020-9 is 2020-09-30, Wednesday
last day of 2020-10 is 2020-10-31, Saturday - last weekday of 2020-10 is 2020-10-30, Friday
last weekday of 2020-11 is 2020-11-30, Monday
last weekday of 2020-12 is 2020-12-31, Thursday
I need to get the date of the next thursday.
So far, I'm using strtotime('next thursday'); to got it. It worked well until I realise it gives me the date of thursday next week if we are thursday today...
What would be the best way to get the next thursday including today's date in calculation ?
You can try this:
if((int) date('N') === 4) {
return date('Y-m-d'); // today is thursday
}
else {
return date('Y-m-d', strtotime('next thursday'));
}
Here you go
if( date( 'N' ) == 4 )
{
echo date( 'd-m-Y' );
}
echo date( 'd-m-Y', strtotime( 'next thursday' ) );
Every Sunday, the strtotime function is doing weird stuff on my schedule website. It shows next week's date instead of +2 weeks.
My code:
date_default_timezone_set("Europe/Amsterdam");
$time = time();
//Check the date of Monday and Sunday from 2 weeks later.
$monday = date( 'd-m-Y', strtotime( '+1 week monday' ) );
$sunday = date( 'd-m-Y', strtotime( '+2 week sunday' ) );
//Check the weeknumber of this week and add 2, so that it shows the weeknumber of 2 weeks later.
$weekdata = date("W", strtotime('+1 day', $time));
$weeknumber = $weekdata + 2;
$Weeknumber shows correct information: 42, but $monday and $sunday shows next week's monday and sunday (03-10-2016 | 09-10-2016), but I want to have it show (10-10-2016 | 16-10-2016)
Also, is there a way to make the weeks start at monday instead of sunday? Because, on the "this week" page, it already shows data from week 26-09-2016 | 02-10-2016 instead of 19-09-2016 | 25-10-2016.
The strtotime function bug happens on PHP version 5.5.31. Upgrade your PHP version or try the fix below.
So I've fixed the bug with an if/else statement.
$time = time();
//Check the date of monday and sunday from this week.
//Strtotime() bug on PHP version 5.5.31. Bug report: https://bugs.php.net/bug.php?id=63740
if(date("l") == "Sunday"){
$monday = date( 'd-m-Y', strtotime( 'monday last week' ) );
$sunday = date( 'd-m-Y', strtotime( 'sunday last week' ) );
//Check the weeknumber of this week.
$weeknumber = date("W", strtotime('+0 day', $time));
} else {
$monday = date( 'd-m-Y', strtotime( 'monday this week' ) );
$sunday = date( 'd-m-Y', strtotime( 'sunday this week' ) );
//Check the weeknumber of this week.
$weeknumber = date("W", strtotime('+1 day', $time));
}
Since the bug only happened on sunday, I made the if/else statement only check for sunday and not for the other days. If today is not sunday, it runs the else code, which is the normal way of getting this week's data. I'll see if the code works 100% when today is not sunday, but another day of the week.
I just wanted to show my solution to this problem to the people who's going to visit this question in the future.
I am having a small issue where I have the month (December 2013) and the Week Number (1, 2, 3 etc) and I need to find out the first date of each week.
I cannot do a simple search like finding the closest Sunday relative to a date since some weeks don't start on a Sunday at all.
Any help would be appreciated.
Considering you have the date of 1st of any month :
First find the next Sunday
Then add 1 week at a time to it to get subsequent Sunday's
Code :
$firstDayOfMonth = '2013-12-01'; // Try also with first day of other months
$week1 = $firstDayOfMonth;
$week2 = date( "Y-m-d" ,strtotime('next Sunday', strtotime( $week1 ) ) );
$week3 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week2 ) ) );
$week4 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week3 ) ) );
$week5 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week4 ) ) );
echo '
Week 1 starts on : ' .$week1 .'<br>
Week 2 starts on : ' .$week2 .'<br>
Week 3 starts on : ' .$week3 .'<br>
Week 4 starts on : ' .$week4 .'<br>
Week 5 starts on : ' .$week5 .'<br>';
Output :
Week 1 starts on : 2013-12-01
Week 2 starts on : 2013-12-08
Week 3 starts on : 2013-12-15
Week 4 starts on : 2013-12-22
Week 5 starts on : 2013-12-29
Just try
<?php
$first_day_this_month = date('M-D-Y',strtotime(date('M-01-Y')));
echo date('M-D-Y',strtotime(date('M-01-Y')))." = ".date('M-d-Y',strtotime(date('M-01-Y')));//First day of month
echo "<br>";
$last_day_this_month = date('t');//last day of month
$last_day_num=intval($last_day_this_month);
for($i=1;$i<$last_day_num;$i++)
{
$first_day_this_month = date('M-D-Y',strtotime(date('M-'.$i.'-Y')));//All day of month
$dateopt_arr=date('M-N-Y',strtotime(date('M-'.$i.'-Y')));
$dateo=explode('-',$dateopt_arr);
//echo $dateo[1];
if(($dateo[1]%7)==0)
{
echo $first_day_this_month = date('M-D-Y',strtotime(date('M-'.$i.'-Y')))." = ".date('M-d-Y',strtotime(date('M-'.$i.'-Y')));//Week day of month
echo "<br>";
}
}
?>
Use the second argument of strtotime to specify the date to calculate "next Sunday" and other relative dates from:
strtotime('next Sunday', strtotime('09/12/2013'));
http://us2.php.net/strtotime
Example:
echo date('m/d/Y', strtotime('next Sunday', strtotime('09/12/2013')));
How can I get the date for monday and friday for the current week?
I have the following code, but it fails if current day is sunday or saturday.
$current_day = date("N");
$days_to_friday = 5 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("Y-m-d", strtotime("- {$days_from_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_to_friday} Days"));
Best solution would be:
$monday = date( 'Y-m-d', strtotime( 'monday this week' ) );
$friday = date( 'Y-m-d', strtotime( 'friday this week' ) );
These strtotime inputs work very well:
strtotime( "next monday" );
strtotime( "previous monday" );
strtotime( "today" );
strtotime( "next friday" );
strtotime( "previous friday" );
All you need to do is to wrap the logic inside some if statements.
This question needs a DateTime answer:-
/**
* #param String $day
* #return DateTime
*/
function getDay($day)
{
$days = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 3, 'Thursday' => 4, 'Friday' => 5, 'Saturday' => 6, 'Sunday' => 7];
$today = new \DateTime();
$today->setISODate((int)$today->format('o'), (int)$today->format('W'), $days[ucfirst($day)]);
return $today;
}
Usage:
var_dump(getDay('Monday')->format('l dS F Y'));
var_dump(getDay('Friday')->format('l dS F Y'));
Output:
string 'Monday 30th September 2013' (length=26)
string 'Friday 04th October 2013' (length=24)
See it working
i use :
$first_week_date = date('d F Y', strtotime('next Monday -1 week', strtotime('this sunday')));
$last_week_date = date('d F Y', strtotime('next Monday -1 week + 4 days', strtotime('this sunday')));
This really depends on how you define a week but I came up with this function that will give you the date for the nearest "monday" or "friday" (or any day for that matter):
function closestDate($day){
$day = ucfirst($day);
if(date('l', time()) == $day)
return date("Y-m-d", time());
else if(abs(time()-strtotime('next '.$day)) < abs(time()-strtotime('last '.$day)))
return date("Y-m-d", strtotime('next '.$day));
else
return date("Y-m-d", strtotime('last '.$day));
}
Input: a day of the week ("sunday", "Monday", etc.)
Output: If I asked for the nearest "sunday" and today is:
"Sunday": I will get today's date
"Monday": I will get yesterday's date
"Saturday: I will get tomorrow's date
Hope this helps :)
As the top answer suggests, using PHP's strtotime() function is the easiest way.
However, instead of using if statements as he suggests, you could simply reset back to the previous Sunday and grab the dates you require from there.
$monday = strtotime('next monday', strtotime('previous sunday'));
$friday = strtotime('next friday', strtotime('previous sunday'));
I needed a definition of the current week per ISO 8601. I want Monday to always be defined as the Monday that started this current week.
The following solution works excellent for me:
$monday = strtotime(date('o-\WW'));
$friday = strtotime("next friday",$monday);
For $monday, this method will always return the Monday that started this calendar week. unfortunately, this method relies on PHP 5.1 to parse the o date format.
To get any day of the week, you could try:
function time_for_week_day($day_name, $ref_time=null){
$monday = strtotime(date('o-\WW',$ref_time));
if(substr(strtoupper($day_name),0,3) === "MON")
return $monday;
else
return strtotime("next $day_name",$monday);
}
Usage:
time_for_week_day('wednesday');
time_for_week_day('friday',strtotime('2014-12-25'));
I was looking for a similar thing, except I wanted any Monday, not just this week. This is what I came up with:
function getSunday(DateTime $date){
$outdate = clone($date);
$day = $date->format("w"); // get the weekday (sunday is 0)
$outdate->sub(new DateInterval("P".$day."D")); // subtracting the weekday from the date always gives Sunday
return $outdate;
}
It accepts an arbitrary date and gives the Sunday. Then you can easily add back days to get Monday through Saturday.
get the current week
$week = [];
$saturday = strtotime('saturday this week');
foreach (range(0, 6) as $day) {
$week[] = date("Y-m-d", (($day * 86400) + $saturday));
}