PHP 将时间变量与 if/else 进行比较
PHP compare time variables with if/else
我正在尝试使用 if/else
语句根据时间戳的结果回显一些文本。
我的逻辑是这样的;
基于$data
的值;
- If no results => echo 'no results'
- 如果日期 小于 今天的日期 => echo 'expired'
- Else => echo 'active'
我面临的问题是目前我的代码总是显示 'active' 无论日期是什么 - 我的代码到目前为止是;
<?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 无效
尝试
American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006",
"1/17/6"
或
Day, month and four digit year, with dots, tabs or dashes dd [.\t-] mm
[.-] YY "30-6-2008", "22.12\t1978"
Day, month and two digit year, with
dots or tabs dd [.\t] mm "." yy "30.6.08", "22\t12\t78"
我正在尝试使用 if/else
语句根据时间戳的结果回显一些文本。
我的逻辑是这样的;
基于$data
的值;
- If no results => echo 'no results'
- 如果日期 小于 今天的日期 => echo 'expired'
- Else => echo 'active'
我面临的问题是目前我的代码总是显示 'active' 无论日期是什么 - 我的代码到目前为止是;
<?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 无效
尝试
American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006", "1/17/6"
或
Day, month and four digit year, with dots, tabs or dashes dd [.\t-] mm [.-] YY "30-6-2008", "22.12\t1978"
Day, month and two digit year, with dots or tabs dd [.\t] mm "." yy "30.6.08", "22\t12\t78"