Getting first monday of month from given datetime dosenot retain the time component

datetime, php

Solution

If you want to retain the time at 10:00, try:

$test = new DateTime('2013-08-02 10:00');
echo $test->modify('first monday ' . $test->format('H:i'))->format('Y-m-d H:i');

Problem

What I am Trying Reference: Get first monday of month for a given set of datetimes I am trying to get the first monday of month based on the datetimes given. Eg: Consider the datetimes `2013-08-02 00:00; 2013-09-05 00:00`. Then the result will be `2013-08-05 00:00; 2013-09-02 00:00` respectively. I am able to do upto this point but it dosenot retain the time component. What I require In the above eg: if the two datetimes contains a time value say `2013-08-02 10:00; 2013-09-05 10:00`. Then the output should be `2013-08-05 10:00; 2013-09-02 10:00`. But I am getting `2013-08-05 00:00; 2013-09-02 00:00` from my code. What can I possibly do to solve this? Code ``` $test = new DateTime('2013-08-02 10:00'); echo $test->modify('first monday')->format('Y-m-d H:i'); ```

Original source

Related problems