Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get date for monday and friday for the current week (PHP)

Tags:

date

php

datetime

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")); 
like image 561
Psyche Avatar asked Sep 16 '09 08:09

Psyche


People also ask

How can I get Monday of current week in PHP?

Try this. echo date('Y-m-d', strtotime('last monday', strtotime('next monday'))); It will return current date if today is monday, and will return last monday otherwise.

How can I get current date from next week in PHP?

just used it - date('Y-m-d H:i:s', strtotime('+1 week')); works fine in PHP 7 to give one week from now.

How can I get current week no in PHP?

How to get the week number from a date. To get the ISO week number (1-53) of a date represented by a Unix timestamp, use idate('W', $timestamp ) or strftime('%-V', $timestamp ) .

How can get first and last day of current week in PHP?

$givenday = date("w", mktime(0, 0, 0, MM, dd, yyyy)); This gives you the day of the week of the given date itself where 0 = Sunday and 6 = Saturday. From there you can simply calculate backwards to the day you want. This answer returns whether a given date is sunday or monday etc.


2 Answers

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.

like image 30
Salman A Avatar answered Sep 21 '22 07:09

Salman A


Best solution would be:

$monday = date( 'Y-m-d', strtotime( 'monday this week' ) ); $friday = date( 'Y-m-d', strtotime( 'friday this week' ) ); 
like image 119
GDY Avatar answered Sep 21 '22 07:09

GDY