Getting the first month of the month from a given datetime dosenot save the time component

What am i trying

Link: Get the first month of the month for a given set of dates.

I am trying to get the first Monday of the month based on data dates.

For example: Consider the dates 2013-08-02 00:00; 2013-09-05 00:00 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 2013-08-05 00:00; 2013-09-02 00:00 respectively.

I can do this up to this point, but dosenot saves the time component.

What I need

In the above example: if two datetimes contain a time value, say 2013-08-02 10:00; 2013-09-05 10:00 2013-08-02 10:00; 2013-09-05 10:00 . Then the exit should be 2013-08-05 10:00; 2013-09-02 10:00 2013-08-05 10:00; 2013-09-02 10:00 . But I get 2013-08-05 00:00; 2013-09-02 00:00 2013-08-05 00:00; 2013-09-02 00:00 from your code. What can I do to solve this problem?

the code

  $test = new DateTime('2013-08-02 10:00'); echo $test->modify('first monday')->format('Ymd H:i'); 
+4
source share
2 answers

If you want to save time at 10:00, try:

 $test = new DateTime('2013-08-02 10:00'); echo $test->modify('first monday ' . $test->format('H:i'))->format('Ymd H:i'); 
+3
source
 Try this: echo strftime("%d/%m/%Y", strtotime("first Monday of Aug 2013")); 
+1
source

Source: https://habr.com/ru/post/1495510/


All Articles