/    Sign up×
Community /Pin to ProfileBookmark

Remarks on Finding last week’s day

Hey guys,
I am trying to determine the last week’s monday in the format: month DD, YYYY HH:MM:SS. Thus, I came up with this function:

[code=php]
/******************************************************/
/* GETLASTWEEKDAY *************************************/
/******************************************************/
/* FORMAT: HOW THE DAY IS SUPPOSED TO LOOK ************/
/******************************************************/
function getLastWeekDay($format) {
$day = date(“w”);

/* SET DATE TO A WEEK AGO FROM THE LAST MONDAY */
switch ($day) {
case ‘1’:
/* MONDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+0))).” 00:00:00″);
break;
case ‘2’:
/* TUESDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+1))).” 00:00:00″);
break;
case ‘3’:
/* WEDNESDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+2))).” 00:00:00″);
break;
case ‘4’:
/* THURSDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+3))).” 00:00:00″);
break;
case ‘5’:
/* FRIDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+4))).” 00:00:00″);
break;
case ‘6’:
/* SATURDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+5))).” 00:00:00″);
break;
case ‘0’:
/* SUNDAY */
$lastWeek = date($format, (time()-(60*60*24*(7+6))).” 00:00:00″);
break;
}

return $lastWeek;
}
[/code]

It’s fairly simple, but I was wondering how other people did it, if there is a better way, etc.
I looked at some calendar functions, namely JDDayOfWeek(), but I couldn’t come up with a way to use it either without using a switch statement. I tested it for today, it works fine, so I can’t think anything other than it working for the other days. But if you guys have a better way, I’m interested.

Jaelan

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 18.2005 — Well, I think I got it down to 2 lines. ?
[code=php]
function getLastWeekDay($format) {
$day = (date('w') == 0) ? 6 : date('w') - 1;
return(date($format, (time()-(60*60*24*(7+$day)))." 00:00:00"));
}
[/code]
Copy linkTweet thisAlerts:
@JonaJan 18.2005 — [font=trebuchet ms]You function could better be written as:[/font]

[code=php]
function getLastWeekDay($format) {
return date($format, (time()-(60*60*24*(7+(date("w")))));
}
[/code]


[font=trebuchet ms]You might want to look into the [url=http://www.php.net/mktime]mktime()[/url] function, though.

[i]Edit: NogDog beat me and also caught something I missed.[/i][/font]
Copy linkTweet thisAlerts:
@JaelanauthorJan 18.2005 — BRILLIANT!!!

Jaelan
Copy linkTweet thisAlerts:
@NogDogJan 20.2005 — Check out the strtotime() function. I just ran across this at http://us3.php.net/manual/en/function.strtotime.php while researching another question:
[code=php]
echo strtotime("last Monday"), "n";
[/code]
Copy linkTweet thisAlerts:
@JaelanauthorJan 20.2005 — lol, that's awesome! perfect! ?

and this is why php is cool
×

Success!

Help @Jaelan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...