我正在尝试使用if/else
语句根据时间戳的结果回显一些文本。
我的逻辑如下;
基于$data
;
我面临的问题是,目前我的代码总是显示“有效”,无论日期是什么 - 到目前为止我的代码是;
<?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
}
?>
我确信答案对于专业人士来说是显而易见的,但你在这里有一个新手!
答案 0 :(得分:1)
尝试这样的事情:
<?php
function myDateToTs($date){
$data_array = explode("-",$date);
return strtotime('20'.$data_array[2].'-'.$data_array[1].'-'.$data_array[0]);
}
$data = "31-12-13";
$dataTimestamp = myDateToTs($data);
$now = strtotime('today');
//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';
}
//everything else
else {
echo 'active';
}
答案 1 :(得分:0)
日期格式dd-mm-yy格式对于strtime无效
尝试
美国月,日和年mm“/”dd“/”y“12/22/78”,“1/17/2006”, “1/17/6”
或
日,月和四位数年份,带点,标签或短划线dd [。\ t-] mm [.-] YY“30-6-2008”,“22.12 \ t1978”
日,月和两位数年份 点或标签dd [。\ t] mm“。” yy“30.6.08”,“22 \ t12 \ t78”