Jump to content

[SOLVED] Getting Yesterdays Date with a twist.


iPixel

Recommended Posts

So... i want to get yesterdays date.

 

that's easy.

 

<?php
### Figure Out Yesterdays Date ###
$xtap_yesterday = mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-1,$xtap_date['year']);
echo date('M-j-Y',$xtap_yesterday);
?>

 

My issue is, i dont care for weekends, so if today is Monday then yesterday should be friday, and if today is wednesday then yesterday is tuesday.

 

My best guess at this is somehow loop todays date, and subtract 1 from each go-around, and everytime i get a date MM-DD-YYYY test it against mktime or something and see if it's a weekend. But i'm not sure how.

 

This also should take into consideration if let's say to day is October 1st, then yesterday should be September 30th, while still keeping weekends omitted in mind.

 

Anybody have a clue how i could get my hands on this one?

tell me if this works for you:

<?php
### Figure Out Yesterdays Date ###
$xtap_yesterday = mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-1,$xtap_date['year']);
print date("M-j-Y",$xtap_yesterday)."\n";
$yesterday = date("w",$xtap_yesterday);
switch ($yesterday){
case "6":
	$xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-2,$xtap_date['year']);
	break;
case "7":
	$xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-3,$xtap_date['year']);
	break;
default:
}
echo date('M-j-Y',$xtap_yesterday);
?>

Not sure if it's the best solution, but this would probably work...

 

$yesterday = date("l", strtotime("yesterday"));
if($yesterday == "Saturday" || $yesterday == "Sunday") {
echo date("M-j-Y", strtotime("last friday"));
} else {
echo date("M-j-Y", strtotime("yesterday"));
}

Not sure if it's the best solution, but this would probably work...

 

$yesterday = date("l", strtotime("yesterday"));
if($yesterday == "Saturday" || $yesterday == "Sunday") {
echo date("M-j-Y", strtotime("last friday"));
} else {
echo date("M-j-Y", strtotime("yesterday"));
}

 

Bah! I was just using his existing code. You had to ruin my horrible code that way *sigh*

That prints Oct-18-2009 Oct-18-2009  when it should pring Oct-16-2009 Oct-16-2009

 

tell me if this works for you:

<?php
### Figure Out Yesterdays Date ###
$xtap_yesterday = mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-1,$xtap_date['year']);
print date("M-j-Y",$xtap_yesterday)."\n";
$yesterday = date("w",$xtap_yesterday);
switch ($yesterday){
case "6":
	$xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-2,$xtap_date['year']);
	break;
case "7":
	$xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-3,$xtap_date['year']);
	break;
default:
}
echo date('M-j-Y',$xtap_yesterday);
?>

Following on from cags's post, strtotime is your friend but it can be done much simpler using the phrase previous weekday.  As usual with strtotime, if you want to make it relative to a specific date (defaults to today) then just provide the timestamp as a second argument.

 

Magic

$last_weekday = strtotime("previous weekday");
echo "Previous weekday was " . date("D d M");

 

 

Just as a quick test, try:

foreach (range(1,21) as $dom) {
$today = strtotime("$dom Oct 2009");
$wkday = strtotime("previous weekday", $today);
echo "Previous weekday for " . date("D d M", $today) . " is " . date("D d M\n", $wkday);
}

 

As they say... Like a glove!!! TY it seems to be working. Unfortunately i wont know 100% untill tomorrow :) haha. But it got oct-16-2009. I never though of using strtotime().

 

Not sure if it's the best solution, but this would probably work...

 

$yesterday = date("l", strtotime("yesterday"));
if($yesterday == "Saturday" || $yesterday == "Sunday") {
echo date("M-j-Y", strtotime("last friday"));
} else {
echo date("M-j-Y", strtotime("yesterday"));
}

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.