[英]How to make custom changes in date and time differences?
I want to echo some date only when they are closer like. 我想在他们更接近的时候回应一些约会。
I had created one script and in that I had used following code. 我创建了一个脚本,并且我使用了以下代码。
$now = new DateTime(null, new DateTimeZone('UTC'));
$todaydate=$now->format('Y-m-d H:i:s');
$expdate="2020-09-13 21:00:00";
function format_interval(DateInterval $interval) {
$result = "";
if ($interval->y) { $result .= $interval->format("%y years "); }
if ($interval->m) { $result .= $interval->format("%m months "); }
if ($interval->d) { $result .= $interval->format("%d days "); }
if ($interval->h) { $result .= $interval->format("%h hours "); }
if ($interval->i) { $result .= $interval->format("%i minutes "); }
if ($interval->s) { $result .= $interval->format("%s seconds "); }
return $result;
}
$exp_date = new DateTime($expdate);
$today_date = new DateTime($todaydate);
$difference = $exp_date->diff($today_date);
echo "Days left to expiry:".format_interval($difference)."</br>";
It gives (aprox result) 它给出了(aprox结果)
Days left to expiry: 4 years 10 months 18 days 8 hours 0 minutes 40 seconds 剩余天数:4年10个月18天8小时0分40秒
now I want to show this only when it is expiring in 2 months means 1 months 30 days or less ...... 现在我只想在2个月到期时显示这个意味着1个月30天或更短......
I tried 我试过了
if ("%m"<=1){
echo "Days left to expiry:".format_interval($difference)."</br>";
}
but it didn't worked? 但它没有奏效?
Any help will be highly appriciated. 任何帮助都会受到高度关注。
Thanks. 谢谢。
Months is a little more difficult and would require additional logic, but you could base this on days which is provided by the DateInterval
class. 几个月有点困难,需要额外的逻辑,但你可以根据
DateInterval
类提供的日期。
You could change the logic like so: 您可以像这样更改逻辑:
// Average 60 days over 2 months
if($difference->days <= 60) {
echo "Days left to expiry:".format_interval($difference)."</br>";
}
@steadweb @steadweb
<?php
$expdate="2020-09-13 21:00:00";
$exp_date = new DateTime($expdate);
print $exp_date->format('d M. Y H:i:s');
?>
yes it worked :) 是的它有效:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.