/    Sign up×
Community /Pin to ProfileBookmark

finding the nearest date()

say i am on friday or saturday
and i want to find the closest sunday or last fridat date’s
how can i do it?
thnaks in advance
peleg

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 14.2005 — The last friday is pretty simple (see [url=http://us4.php.net/manual/en/ref.datetime.php]date functions[/url] for more info):
[code=php]
<?php
function get_last_friday()
# returns yyyy-mm-dd; today if it's a Friday, else the last friday
{
$time = time();
while(date("w", $time) != 5)
{
$time -= 24*60*60;
}
return(date("Y-m-d", $time));
}
?>
[/code]

The nearest Sunday would be a bit trickier:
[code=php]
<?php
function get_nearest_sunday()
# returns yyyy-mm-dd
{
$time = time();
if(date("w", $time) == 0)
{
$sun = date("Y-m-d", $time);
}
else
{
$tlast = $time;
$tnext = $time;
while(date("w", $tlast) != 0)
{
$tlast -= 24*60*60;
}
while(date("w", $tnext != 0)
{
$tnext += 24*60*60;
}
$sun = ($tnext - $time < $time - $tlast) ? $tnext : $tlast;
}
return($sun);
}
?>
[/code]

[i]Caveat: all code above is untested.[/i]
Copy linkTweet thisAlerts:
@pelegk1authorJan 16.2005 — second i found this :
[code=php]
echo date('Y-m-d', strtotime('-1 week last thursday')) . "n";
echo date('Y-m-d', strtotime('next sunday')) . "n";
echo date('Y-m-d', strtotime('this sunday')) . "n";

[/code]
×

Success!

Help @pelegk1 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...