[英]PHP difference in days between two date strings
I have these two strings:我有这两个字符串:
20/3/2020
30/3/2020
And I want to get the difference in days between them so I use this code:我想知道它们之间的天数差异,所以我使用以下代码:
$dateS = DateTime::createFromFormat("d/M/Y", "20/3/2020");
$dateE = DateTime::createFromFormat("d/M/Y", "30/3/2020");
echo $dateE->diff($dateS)->days;
But I always get a crash with this code但我总是会因为这段代码而崩溃
Fatal error: Uncaught Error: Call to a member function diff() on bool致命错误:未捕获的错误:调用 bool 上的成员 function diff()
Any idea what can be the problem?知道可能是什么问题吗?
Your date format string is incorrect.您的日期格式字符串不正确。 M
is a format parameter meaning M
是格式参数含义
A short textual representation of a month, three letters ex.一个月的简短文本表示,三个字母 ex。 Jan through Dec 1 月至 12 月
If your months will not have a leading zero you need to use n
:如果您的月份没有前导零,则需要使用n
:
<?php
$dateS = DateTime::createFromFormat("d/n/Y", "20/3/2020");
$dateE = DateTime::createFromFormat("d/n/Y", "30/3/2020");
echo $dateE->diff($dateS)->days;
Output: Output:
10
See the manual for what each format parameter represents.请参阅手册了解每个格式参数所代表的含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.