[英]Best way to record date with mandatory year and month, and only optional day
[英]Date difference only considering year month day
我想只考慮年,月和日在php中計算日期差異。 現在,程序將考慮所有日期時間對象參數來計算日期差。
$DTObj = new DateTime();
$pdate = DateTime::createFromFormat("ymd", 141118);
$diff = $DTObj->diff($pdate);
$dday = $diff->format("%a");
我怎樣才能做到這一點。
閱讀並嘗試以下方法:
# create DateTime object with time at 00:00:00
$from = new DateTime('today');
# create DateTime object from ymd format
# format character ! resets all fields (year, month, ...) to the Unix Epoch and then sets the inputed date
$to = DateTime::createFromFormat('!ymd', '141118');
# calculate difference between $from and $to and return DateInterval object
$diff = $from->diff($to);
# echo difference of total number of days
echo $diff->format('%a');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.