When? Last week? Last year?

Posted on: January 20, 2011 Posted by: John Huh Comments: 0

When? Last week? Last year?

There are times I always write something quick to find out when was the last week’s Sunday or get starting and ending dates of last month. Every time I’d try to remember how I did it before and spend minutes, since I wouldn’t use it so often.

I guess that’s why I leave notes and snippets on this blog (duh…). Well.. here it is.

[php]

switch ($duration)
{
ocase ‘week’:
o$start_date = date(‘Y-m-d’, strtotime("last Sunday"));
o$end_date = date(‘Y-m-d’, strtotime("this Saturday"));
obreak;
ocase ‘last week’:
o$start_date = date(‘Y-m-d’, strtotime("-2 weeks Sunday"));
o$end_date = date(‘Y-m-d’, strtotime("-1 weeks Saturday"));
obreak;
ocase ‘month’:
o$start_date = date(‘Y-m-01’);
o$end_date = date(‘Y-m-t’);
obreak;
ocase ‘last month’:
o$start_date = date(‘Y-m-01’, strtotime("-1 months"));
o$end_date = date(‘Y-m-t’, strtotime("-1 months"));
obreak;
ocase ‘year’:
o$start_date = date(‘Y-01-01’);
o$end_date = date(‘Y-12-31’);
obreak;
ocase ‘last year’:
o$start_date = date(‘Y-01-01’, strtotime("-1 years"));
o$end_date = date(‘Y-12-31’, strtotime("-1 years"));
obreak;
ocase ‘two years ago’:
o$start_date = date(‘Y-01-01’, strtotime("-2 years"));
o$end_date = date(‘Y-12-31’, strtotime("-2 years"));
obreak;
odefault:
obreak;
}

[/php]

Leave a Reply:

Your email address will not be published. Required fields are marked *