I'm writing a php script to display as follows:
December 2020 2020-12-01 2020-12-31 January 2021 2021-01-01 2021-01-31 Febuary 2021 2021-02-01 2021-02-28 March 2021 2021-03-01 2021-03-31 Basically to display date from the very beginning and ends with the latest month. But i got an error upon executing.
Fatal error: Call to a member function format() on boolean From my understanding, DateTime::createFromFormat is supposed to parse any string based on a given format to another format that can be found on date() But instead i got error and I tried old ways like using date("Y-m-01", strtotime($month)) but php outputs 1970-01-01 and 1970-01-31
My code:
$veryfirstmonth = "2020-12-01 18:01:15"; $veryfirstmonth = date('Y-m-d', strtotime($veryfirstmonth)); $today = date("Y-m-d"); $start = (new DateTime($veryfirstmonth))->modify('first day of this month'); $end = (new DateTime($today))->modify('first day of this month'); $interval = DateInterval::createFromDateString('1 month'); $period = new DatePeriod($start, $interval, $end); foreach ($period as $dt) { echo $month = $dt->format("F Y") . "<br>\n"; //Convert $month into start of month and end of month again // echo $start = date("Y-m-01", strtotime($month)); // echo $end = date("Y-m-t", strtotime($month)); $start = DateTime::createFromFormat("F Y", $month)->format("Y-m-01"); $end = DateTime::createFromFormat("F Y", $month)->format("Y-m-t"); echo "Start: " . $start . "\n"; echo "End: " . $end . "\n\n"; } https://stackoverflow.com/questions/67223042/datetimecreatefromformat-not-working-as-intended April 23, 2021 at 10:03AM
没有评论:
发表评论