我正试图根据时间戳的结果,使用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
}
?>
我相信答案对专业人士来说是显而易见的,但你这里有一个新手!
试试这样的东西:
<?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';
}
日期格式dd-mm-yy格式对strtime 无效
尝试
美国月份、日期和年份mm"/"dd"/"y"12/22/78","2006年1月17日","1/17/6"
或
日期、月份和四位数年份,带点、制表符或短划线dd[.\t-]mm[.-]YY"30-6-2008","22.12\t1978"
天、月和两位数的年份点或制表符dd[.\t]mm"."yy"30.6.08","22\t12\t78"
http://php.net/manual/es/datetime.formats.date.php