0
votes

I need to get date of 10th day of current month. This way is not working:

<?php 
$date_start = new DateTime();
$date_start->modify('tenth day of this month');
echo $date_start->format('Y-m-d H:i:s'), "\n";
?>

Results:

Warning: DateTime::modify(): Failed to parse time string (tenth day of this month ) at position 10 (o): The timezone could not be found in the database in [...][...] on line 3

Of cource, I can use date('Y-m-10') but I need to make it with DateTime() object.

3

3 Answers

2
votes

I found correct way:

<?php
$date_start = new DateTime();
$date_start->modify('first day of this month');
$date_start->modify('+9 days');
echo $date_start->format('Y-m-d');
?>

Result on 2018-09-30:

2018-09-10
0
votes

i don't think there is tenth day of this month there is first day of this month :

If you want to do it with DateTime that way, you try double modify something like this:

  $date_start = new DateTime();
  $date_start->modify('first day of this month');
  $date_start->modify('+9 day');
  echo $date_start->format('Y-m-d H:i:s'), "\n";
0
votes

You could use setDate:

$date_start->setDate($date_start->format('Y'), $date_start->format('m'), 10);
echo $date_start->format('Y-m-d H:i:s'), "\n";

Output:

2018-10-10 06:49:01