Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime modify string for first day of current year

Tags:

php

datetime

im looking for the DateTime modify String for the first day of the year (now 1. January 2011). I tried the following:

<?php

$time = new DateTime();

// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";


// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";

>

I know there are other ways to retrieve the date, but I search an string for DateTime->modify() no other solution.

like image 311
Marko Avatar asked Oct 28 '11 10:10

Marko


2 Answers

You should specify the year too, as you can see in this example:

"first day of January 2008"

from the official doc.

Update: It works on php version >= 5.3.6

like image 57
Aurelio De Rosa Avatar answered Nov 11 '22 07:11

Aurelio De Rosa


On v5.5.6

echo date('Y-m-d', strtotime('first day of January this year'));

Result: 2013-01-01

like image 31
user2439480 Avatar answered Nov 11 '22 06:11

user2439480