Jump to content

[SOLVED] Working out the date - beginning of this week - beginning of last month


andrewgarn

Recommended Posts

Right the requirements for my website i'm making are finding the date:

 

<> at the beginning of this week (last monday or today if its a monday today)

<> at the beginning of last week (monday)

 

<> beginning of this month - (1st of month)

<> beginning of last month - (1st of month)

 

I can get todays date by doing:

 

$yesterday = date("Y-m-d", strtotime("-1  days"));

 

but i'm not sure about what to do for the rest, any ideas?

 

so if this isnt very clear:

 

today is 2008-06-20

 

I would want to calculate the date:

 

the monday this week -> 2008-06-16

monday last week -> 2008-06-09

 

first day of this month -> 2008-06-01

first day of last month -> 2008-05-01

See it this helps:

<?php
$tmp = array();
$tmp[] = 'Today ' . date('Y-m-d') . ' is a ' . date('l');
$begweek = (date('l') == 'Monday')?date('Y-m-d'):date('Y-m-d',strtotime('last monday'));
$tmp[] = 'The beginning of the week was ' . $begweek;
$tmp[] = 'The beginning of last week was ' . date('Y-m-d',strtotime('-2 monday'));
$tmp[] = 'The start of this month was ' . date('Y-m-01') . ', a ' . date('l',strtotime(date('Y-m-01')));
$tmp[] = 'The start of last month was ' . date('Y-m-01',strtotime('last month')) . ', a '. date('l',strtotime(date('Y-m-01',strtotime('last month'))));
echo implode("<br>\n",$tmp);
?>

 

Ken

See it this helps:

<?php
$tmp = array();
$tmp[] = 'Today ' . date('Y-m-d') . ' is a ' . date('l');
$begweek = (date('l') == 'Monday')?date('Y-m-d'):date('Y-m-d',strtotime('last monday'));
$tmp[] = 'The beginning of the week was ' . $begweek;
$tmp[] = 'The beginning of last week was ' . date('Y-m-d',strtotime('-2 monday'));
$tmp[] = 'The start of this month was ' . date('Y-m-01') . ', a ' . date('l',strtotime(date('Y-m-01')));
$tmp[] = 'The start of last month was ' . date('Y-m-01',strtotime('last month')) . ', a '. date('l',strtotime(date('Y-m-01',strtotime('last month'))));
echo implode("<br>\n",$tmp);
?>

 

Ken

 

Thank you, does exactly what i need, should be able to work out any further I need :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.