So I have 5 variables that I use to store last weeks dates using the php function strtotime()
The only problem is, every Sunday the dates return the same dates. Like today for instance, using $monday = date('Y-m-d', strtotime('monday last week'));
will return me a date of 2019-04-08
Obviously being the 2019-04-21 today that should be 2019-04-08.
The problem starts here though, if I use this $tuesday = date('Y-m-d', strtotime('tuesday last week')); it will return me a date of 2019-04-08 and the date today is 2019-04-21 so it should return return me the date 2019-04-09.
The same is true if I try any of the weekdays. They all return me the same date, 2019-04-08.
What's more is its also true if you are using date('Y-m-d', strtotime('monday this week'));
works for monday but returns monday for all of the days of the week when on a Sunday.
I saw someone say use a function, so I was researching on here and tried it but it returned the same result.
//// function I tried but it proved to produce the same results.///////
function lastfriday() {
return date('Y-m-d', date('N')==5 ? strtotime('today') : strtotime('last friday'));
}
////////// this is the code I have been using but as I explained, every Sunday it gets buggy
echo $monday = date( 'Y-m-d', strtotime( 'monday last week' ) );
echo $tuesday = date( 'Y-m-d', strtotime( 'tuesday last week' ) );
echo $wednesday = date( 'Y-m-d', strtotime( 'wednesday last week' ) );
echo $thursday = date( 'Y-m-d', strtotime( 'thursday last week' ) );
echo $friday = date( 'Y-m-d', strtotime( 'friday last week' ) );
echo $saturday = date( 'Y-m-d', strtotime( 'saturday last week' ) );
running that code on today 2019-04-21 produces the results
2019-04-08
2019-04-08
2019-04-08
2019-04-08
2019-04-08
2019-04-08
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' ) );
Is there an easy way to get last week's, say, Monday? If today is Tuesday, I do not want yesterday's Monday. Rather, I want the Monday 8 days ago (last week's Monday). Then I want that Monday's proceeding Sunday. Basically, I'm trying to get the date range for last week, Monday to Sunday.
This doesn't always work right:
date('Y-m-d', strtotime('last Monday')
Suggestions?
You can use “this week” format:
$monday = strtotime( 'this week', strtotime( '7 days ago' ) );
$sunday = strtotime( '+ 6 days', $monday );
3v4l.org demo
“this_week” returns monday of previous week, then — adding 6 days — you obtain the monday of relative week.
The strtotime function accepts the current date as a parameter.
http://php.net/manual/en/function.strtotime.php
Just pass in strtotime('last Sunday') as the parameter to get a weekday of the last full week.
$beginning_of_week = strtotime('last Sunday');
$result = date('Y-m-d', strtotime('last Monday', $beginning_of_week));
echo $result;
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' );
}
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));
}