我在Mysql数据库中存储日期,我正在尝试以年,月,日格式格式化那些日子。我存储日期的变量称为$days
这就是我所拥有的:
$years = ($days / 365);
$years = floor($years);
$month = ($days % 365) / 30.5;
$month = floor($month);
$days = ($days % 365) % 30.5;
$display = "$years" . " years, " . "$month" . " months, and " . "$days" . " days";
然而,每当我有90天的价值时,它只显示为2个月,并且没有显示任何日期,只有2个月。我在网上找到了这个,直到最近才开始工作。
任何人都可以给我一些更好的方法,或者我的代码中有任何错误吗?
答案 0 :(得分:1)
如下所示: -
<?php
$days = '365';
$start_date = new DateTime(date("Y/m/d"));
$end_date = new DateTime(date("Y/m/d",strtotime("+$days days")));
$dd = date_diff($start_date,$end_date);
echo "$dd->y year(s) $dd->m month(s) $dd->d day(s)";
?>
答案 1 :(得分:0)
由于没有指定特定日期,您可以这样做
{
"msg_id": "387b8515-0c1d-42a9-aa80-e68b66b66c27",
"_text": "how many people between Tuesday and Friday",
"entities": {
"metric": [ {
"metadata": "{'code': 324}",
"value": "metric_visitor",
"confidence": 0.9231
} ],
"datetime": [ {
"value": {
"from": "2014-07-01T00:00:00.000-07:00",
"to": "2014-07-02T00:00:00.000-07:00"
},
"confidence": 1
}, {
"value": {
"from": "2014-07-04T00:00:00.000-07:00",
"to": "2014-07-05T00:00:00.000-07:00"
},
"confidence": 1
} ]
}
}
输出将如下:<?php
$days = 500;
$years = intval($days / 365);
$days = $days % 365;
$months = intval($days / 30);
$days = $days % 30;
echo "$years years, $months months, $days days";
?>