Jump to content


Photo

Problem with strtotime()


  • Please log in to reply
4 replies to this topic

#1 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 17 August 2006 - 02:38 PM

This code:

[quote]<?php

$lastjuly = strtotime("Last July 1");
echo $lastjuly;
echo date("m/d/Y", $lastjuly);

?>

.....returns the UNIX timestamp and 06/29/2001    :o

Anybody know why?

Now, if I change it to read "Last Monday," it returns 08/14/2006, so it works, but what's wrong with my syntax?



#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 02:42 PM

because you are echoing both the timestamp and the the formatted version?

#3 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 17 August 2006 - 02:45 PM

because you are echoing both the timestamp and the the formatted version?


Yes, but that's deliberate. Since I'm a total newbie, I tend to echo WAY too many things to see what I'm doing.  :D

The problem is that the formatted version should be giving me 07/01/2006, since that was the last July 1 that happened. Instead, it's giving me 06/29/2001.

See what I mean?

Thanks!


#4 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 02:51 PM

Oh i see, sorry. I guess the string is not explicit enough. You could try last 01 July or something, but again, that might be too ambiguous.

You could try:
<?php
$month = date('m');
if($month >= 6){
$year = date('Y');
}else{
$year = date('Y') -1;
}

$lastjuly = strtotime("01 July $year");
echo $lastjuly;
echo date("m/d/Y", $lastjuly);
?>


#5 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 17 August 2006 - 02:54 PM

Oh i see, sorry. I guess the string is not explicit enough. You could try last 01 July or something, but again, that might be too ambiguous.

You could try:

<?php
$month = date('m');
if($month >= 6){
$year = date('Y');
}else{
$year = date('Y') -1;
}

$lastjuly = strtotime("01 July $year");
echo $lastjuly;
echo date("m/d/Y", $lastjuly);
?>


Yep, that works. It seems to be a derivation of the "fiscal year" code you posted.

Outstanding!

And thanks.

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users