Nasıl DateTime sınıfını kullanarak geçersiz tarih olarak ''02 / 31/2010 yakalayabilirsiniz?

2 Cevap php

Date () Unix Zaman Damgası olumsuz yanları vardır çünkü PHP DateTime sınıfını kullanarak oldum. Ancak, ne yaklaşım 31 gün var ama 31 gün kullanmak çalışmayın ay için geçersiz tarihleri ​​algılar.

Örnek Kod:

try {
$date = new DateTime('02/31/2018');
$formattedDate = $date->format('Y-m-d');
} catch (Exception $e) {}
echo $formattedDate;

Örnek Çıktı:

2018-03-03

Update 1: To use checkdate() I need the component parts of the date. To get that, I will need to instantiate a DateTime object with the date string as the constructor. In the case of '02/31/2018', it will convert it to '03/03/2018' when instantiated. At that point it will be too late to run the component month, day, year through checkdate(). What is the solution?

Update 2: Getting the component part of the dates is easy if I force a specific format (e.g. mm/dd/yyyy), but if I accept all formats accepted by strtotime(), I cannot perform a checkdate before running it through strtotime() -> date() or DateTime::__construct -> DateTime::format().