Reputation: 10704
I have the following line of code
$sunday = date ("m/d", strtotime("last sunday"));
$saturday = date("m/d",strtotime("this saturday"));
but if today is sunday, it will not select today. The rest of the week will be fine, because last sunday would be today. I was thinking to do like:
if (today is not sunday) {set to last sunday }else{ set to today }
similarly for saturday.
Edit:
It it acceptable to do something like, or is there a better way:
$today = date('D', strtotime("today"));
if( $today === 'Sun') {
$sunday = date ("m/d", strtotime("today"));
}else{
$sunday = date ("m/d", strtotime("last sunday"));
}
if ( $today === "Sat"){
$saturday = date ("m/d", strtotime("today"));
}else{
$saturday = date("m/d",strtotime("this saturday"));
}
Upvotes: 0
Views: 88
Reputation: 3729
I'm sure you could have come up with this yourself really, but...
What about:
function today_or_lastday ($day) {
return date('m/d', strtotime("7 days ago")) == date('m/d', strtotime("last $day"))
? date('m/d', strtotime("today"))
: date('m/d', strtotime("last $day"));
}
And:
$sunday = today_or_lastday("sunday");
$saturday = today_or_lastday("saturday");
Upvotes: 1
Reputation: 1601
I would use a line like this to get the last/current Saturday and Sunday:
$sunday = date( 'm/d', time() - ( date( 'w' ) * 3600*24 ) );
$saturday = date( 'm/d', time() - ( ( date( 'w' ) + 1 ) % 7 * 3600*24 ) );
For Sunday we take now - days from Sunday.
date( 'w' ) gives us the weekday (0=Sunday, 1=Monday, ... 6=Saturday) and we subtract the number of days (counted in seconds 3600*24 ) that has past since Sunday.
For Saturday we do the same thing, but add a little maths trickery to adjust for it being day 6.
Upvotes: 0