I am trying to calculate, based on today’s date (24 August 2012), the following three values:
- Date of this month’s first Saturday.
- Date of this month’s third Saturday.
- Date of next month’s first Saturday.
This is how I do it in PHP script:
// Returns August 2012
$this_month = date("F Y");
// Returns September 2012
$next_month = date("F Y", strtotime("next month"));
// Returns August 04th, Saturday
$t_sat_1st=date('U', strtotime($this_month.' First Saturday'));
// Returns August 18th, Saturday
$t_sat_3rd=date('U', strtotime($this_month.' Third Saturday'));
// Returns September 08th, Saturday
$n_sat_1st=date('U', strtotime($next_month.' First Saturday') );
Why is the wrong date returned for the last line of code? I expect it to return September 1st, 2012. What is wrong with my code?
I don’t know why your code doesn’t exactly work, must be the way it is parsed..
but try
Note the ‘of’ it is necessary.
This code will only work in 5.3+
Comment on this page – http://www.php.net/manual/en/datetime.formats.relative.php
I have tested this on both windows and ubuntu both php 5.3 and 5.4 and it works.
If this does not work, try this