Asked  7 Months ago    Answers:  5   Viewed   24 times

Possible Duplicate:
Get first day of week in PHP?

Hi,

I want to find first and last date of current week and last week. Similarly I want to find first and last date of current month and last month.

This has to be done in PHP. Please help.

 Answers

92

strtotime is quite powerful with relative time formats:

strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');

(this only works with PHP 5.3+)

strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');

In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):

mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month

$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month

If you just want to get a string for presentation, then you don't need mktime:

date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Wednesday, March 31, 2021
 
FyodorX
answered 7 Months ago
62

nl_NL is a typical Unix style locale name. On Windows, locales follow a different format. You want "nld", "holland", or "netherlands".

Additionally, date() is not locale-aware. You probably want strftime().

Saturday, May 29, 2021
 
Shibbir
answered 5 Months ago
41

I'm not sure what format you're looking for in your difference but here's how to do it using DateTime

$datetime1 = new DateTime();
$datetime2 = new DateTime('2011-01-03 17:13:00');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $elapsed;
Friday, June 4, 2021
 
Jesse
answered 5 Months ago
22

Create an array to map numeric DOWs to text DOWs.

$dowMap = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

If you need locale support, load the dow of some random date (epoch (0) would be a good date for example) and then for the next 6 days and build the dow map dynamically.

Thursday, June 24, 2021
 
THEK
answered 4 Months ago
71

The behavior is correct. Both date intervals represent same number of days (364). The month and date part represent the period that needs to be added to/subtracted from first date to reach second date:

1981-11-20 minus 1980-11-21 represents an interval of 11 month 30 day
1980-11-21 + 11 month = 1981-10-21
1981-10-21 + 30 day   = 1981-10-21

1980-11-21 minus 1981-11-20 represents an interval of 11 month 29 day
1981-11-20 - 11 month = 1980-12-20
1980-12-20 - 29 day   = 1980-11-21

A simpler example would be Oct 21 ... Dec 20:

  • You need to add 1 month 29 days to Oct 21 to reach Dec 20 (note that month before Dec has 30 days)
  • But ... you need to subtract 1 month 30 days from Dec 20 to reach Oct 21. Oct itself has 31 days which explains the difference.
Sunday, August 15, 2021
 
ala
answered 3 Months ago
ala
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :