Get first monday of month for a given set of datetimes

datetime, php

Solution

Using php Datetime class:

$Dates = array('2013-07-02', '2013-08-05', '2013-09-13');
foreach ($Dates as $Date)
{
    $test = new DateTime($Date);
    echo $test->modify('first monday')->format('Y-m-d');
}

Problem

What I Require I have a certain list of datetimes. I want to get the first monday of each datetimes. eg: Suppose the given datetimes are ``` 2013-07-05 2013-08-05, 2013-09-13 etc. ``` I want to get the first monday of all these datetimes such that the output results in ``` 2013-07-01, 2013-08-05, 2013-09-02 respectively ``` I am actually stuck with this using `stftime`. ``` strftime("%d/%m/%Y", strtotime("first Monday of July 2012")); ```

Original source