php strtotime question

ThatDood

Limp Gawd
Joined
Nov 23, 2006
Messages
436
I'm trying to figure out the best way to handle this situation. I have a website that has a ton of static set dates on it. Every year the dates will need to be adjusted for the upcoming year (ie. Feb 18th 2010 will be Feb 17th 2011). I was thinking of creating a function or maybe just an echo statement saying the third thursday of february 2011. With 2011 being a variable that can be changed in one place.

Code:
$year = "2011";

date('l, M. jS', strtotime('third thursday $year'));

I don't think this code actually works since I'm not sure how to get the variable $year instead of using the system year.

Any help is appreciated.
 
Code:
<?php
echo date('l, M. jS, Y', strtotime("+1 year"));
?>
Echoes the current date plus one year.
Code:
<?php
echo date('l, M. jS, Y', strtotime("Feb 16th +1 year"));
?>
Echoes a set date (in this year) plus one year.
Code:
<?php
$year = 2020;
echo date('l, M. jS, Y', strtotime("Feb 16th ".$year));
?>
Echoes a set date using the year variable.
Can always mix and match depending on what you want to do.
 
Last edited:
Code:
<?php
echo date('l, M. jS, Y', strtotime("+1 year"));
?>
Echoes the current date plus one year.
Code:
<?php
echo date('l, M. jS, Y', strtotime("Feb 16th +1 year"));
?>
Echoes a set date (in this year) plus one year.
Code:
<?php
$year = 2020;
echo date('l, M. jS, Y', strtotime("Feb 16th ".$year));
?>
Echoes a set date using the year variable.
Can always mix and match depending on what you want to do.

Thanks for the help. The problem is that I'm not sure what the day will be depending on the year. It will always be the 3rd thursday of february, but not always the 16th. Does that make sense?
 
You can pass an optional second argument to strtotime(), which must be a timestamp (int), that tells it what to consider to be "now".

Say you want to find the third Thursday of 2011, you can do this:
Code:
$year = date('Y');
$date_begin = strtotime(date("{$year}-12-31"));
echo date('l jS F (Y-m-d)',strtotime('third Thursday',$date_begin));

One thing I noticed, if that if you set now to be say 2011-1-1, it will only look at days after that for things like "first Saturday". In 2011, the first day is Saturday, so if you do this:
Code:
$year = date('Y') + 1;
$date_begin = strtotime(date("{$year}-1-1"));
echo date('l jS F (Y-m-d)',strtotime('first Saturday',$date_begin));
It actually returns January 8th as being the first Saturday and not January 1st.

This correctly gives January 1st as the result of first Saturday of 2011:
Code:
$year = date('Y');
$date_begin = strtotime(date("{$year}-12-31"));
echo date('l jS F (Y-m-d)',strtotime('first Saturday',$date_begin));
 
Back
Top