Jump to content


What day was yesterday


3 replies to this topic

#1 Penaf

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Location:Portugal

Posted 30 August 2006 - 11:02 PM

Here's one problem I can't seem to solve and is important for me now :P

I need to set up a variable that is

$yesterday = $mday - 1 (being $mday the month day between 1-31)

this is problemless if the $mday is between 2 and 32 ... but when $mday = 1 I get $yesterday = 0 !

well ... that's not true :)

Finding out when $yesterday = 31 or 30 is kind of easy too


But when it is 29 or 28 ? this one is crashing my brain.


My solution so far ...
list($wday,$mday,$month,$year,$hour,$minutes) = split("( )",date("w j n Y H i"));

$yesterday = $mday-1;

if($yesterday == "0"){
	 if($month=="12" || $month == "10" || $month=="7" || $month == "5"){
	 $yesterday = "30";
	 }
	 elseif($month=="1" || $month=="2" || $month=="4" || $month=="6" || $month=="8" || $month=="9" || $month=="11" ||){
	 $yesterday = "31";
	 }
	 else{
????????????????????????????
????????????????????????????
????????????????????????????
	}
}


#2 Penaf

    Young Padawan

  • Members
  • Pip
  • 85 posts
  • Location:Portugal

Posted 30 August 2006 - 11:19 PM

well ... i remembered to search google a bit more with other search terms and I found a nice answer to this problem!

Anyway here it is in case anyone has the same problem in the future :)

$yesterday = date("Y-m-d",mktime(0,0,0,date("m") ,date("d")-1,date("Y")));


#3 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 31 August 2006 - 03:05 AM

I recommend you use time() values.

$today = date( 'j, l', time() );
This would return the day of the month 1 to 31, and then the name of the day.

To get yesterday's day:

$yesterday = date( 'j, l', time() - 60*60*24 );
This would return the day of the month + the day of the week of yesterday.
time() - 60(seconds)*60(minutes)*24(hours) is 24 hours.

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 31 August 2006 - 11:46 AM

I swear to god this is the most missed function in the entire php manual.
strtotime()
$yesterday = strtotime('yesterday');

Edited by rc69, 31 August 2006 - 11:46 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users