...

View Full Version : Last Monday's Date Function



LynxGrr
06-09-2004, 05:50 PM
I have this function to work out the date of the monday just past.

Function is called like this

$time = time();


function get_monday($time) {
$currday = date("D", mktime(substr($time,6,2)));
if ($currday == "Mon") {
$new_time = time();
$monday = date("d m Y", mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
echo $monday;
}
else if ($currday == "Tue") {
$new_time = (time() - 86400);
$monday = date("d m Y", mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
echo $monday;
}
else if ($currday == "Wed") {
$new_time = (time() - 172800);
$monday = date("d m Y", mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
echo $monday;
}
else if ($currday == "Thu") {
$new_time = (time() - 259200);
$monday = date("d m Y", mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
echo $monday;
}
else if ($currday == "Fri") {
$new_time = (time() - 345600);
$monday = date("d m Y", mktime(substr($new_time,6,2),substr($new_time,4,2),substr($new_time,0,4)));
echo $monday;
}
}

However when I test it, the variable $monday is set to a day in the future not in the past. Any help on getting the correct date would be greatly appreciated, as would any advice on reducing the number of lines in the function.

MrShed
06-09-2004, 06:37 PM
http://uk2.php.net/manual/en/function.strtotime.php

This function already does it:

eg.

echo strtotime("last Monday");

LynxGrr
06-09-2004, 07:33 PM
he he he... seems theres a lot of PHP functions I still need to learn!

Many thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum