why does <?php echo date("m/d/Y" strtotime('second wednesday June 2016')) ?> output 6/15/2016 instead of 6/8/2016?

287

why does

<?php echo date("m/d/Y" strtotime('second Wednesday June-2016')) ?>  

output 6/15/2016 instead of 6/8/2016?

This is the work around I've come up with --- not very eloquent:

    <?php
    months = array( 1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec');
   $year = "2016";
    $month = "06";
    $smonth=$months[(int)$month];
    $tstr=$month."/01/".$year;
    $dow=date("D",strtotime($tstr));
    if ( $dow == "Wed" ) {
        $nstr="first";
    } else {
        $nstr="Second";
    }
    $edate= date("m/d/Y", strtotime($nstr.' Wednesday '.$smonth.'-'.$year));
    echo $edate."<br>";
    ?>
934

Answer

Solution:

Look at the documentation for relative date formats: https://secure.php.net/manual/en/datetime.formats.relative.php

It looks like they all use the word "of" between the month and day. I tried it on my local server withecho date("m/d/Y", strtotime('second Wednesday of June-2016')); and it worked. It looks like leaving out the "of" causes undefined behavior.

293

Answer

Solution:

You wrote the following:

<?php echo date("m/d/Y" strtotime('second Wednesday June-2016')) ?>

The two problems with it are that there should be a comma after"m/d/Y", although I assume that this typo was only on Stackoverflow. The other problem is that it should saystrtotime('second Wednesday of June 2016')

Leaving out the wordof can cause unknown results.

People are also looking for solutions to the problem: php - Config Symfony3 URL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.