【发布时间】:2015-11-06 14:48:32
【问题描述】:
我正在尝试使用if/else 语句根据时间戳的结果来回显一些文本。
我的逻辑如下;
基于$data的值;
- 如果没有结果 => echo '没有结果'
- 如果日期小于今天的日期 => echo '过期'
- Else => echo '活动'
我面临的问题是,目前我的代码总是显示“活动”,无论日期是什么 - 我的代码到目前为止是;
<?php
$data = get_patronapi_data($id);//this contains a date in the format 31-12-13
$dataTimestamp = strtotime($data);//convert date to timestamp
$now = strtotime('today');set timestamo for $now
//echo strtotime('now'); this outputs 1446820684
//echo $dataTimestamp; this outputs 1954886400
//echo $data; this outputs 31-12-13
//if $data is empty
if (empty($data)){
echo 'no results';
}
//if $data is not empty but date is less than today
elseif (!empty($data) && $dataTimestamp < $now) {
echo 'expired date is' .$date; //in d-m-y format
}
//everything else
else {
echo 'active date is ' .$date; //in d-m-y format
}
?>
我相信对于专业人士来说答案是显而易见的,但你这里有一个新手!
【问题讨论】:
-
提示:
strtotime('today')是在浪费 CPU 时间。就做$now = time()。 -
您是否检查过
$datatimestamp中的值。如果 strtotime 失败,它返回布尔值 false,它将被视为整数0用于您的<测试。 -
调整
get_patronapi_data()以返回2013-12-31而不是31-12-13 -
strtotime('today')不等于一天的开始吗?您需要它是当前的秒数还是一天的开始? -
因为
strtotime不知道你是哪一年。当您使用“-”时,它需要美国格式,您也可以将“-”替换为“。”喜欢31.12.13。他们 strtotime 期望德语(等)格式
标签: php if-statement timestamp strtotime