Reputation: 2749
I am trying to echo some text depending on the results of a timestamp, using an if/else
statement.
My logic is as follows;
Based on the value of $data
;
The problem I am facing is that currently my code always displays 'active' no matter what the date is - my code so far is;
<?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
}
?>
I'm sure the answer will be obvious to a pro but you have a newbie here!
Upvotes: 1
Views: 1820
Reputation: 773
date format dd-mm-yy format is not valid for strtime
Try
American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006", "1/17/6"
or
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"
http://php.net/manual/es/datetime.formats.date.php
Upvotes: 0
Reputation: 748
Try something like this:
<?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';
}
Upvotes: 1