Reputation: 17
I am trying to find difference between two dates (one is user registration date and another is current date). When the current date will be more then one day from registration date then the message will pop up "you are capable to donate blood". But its showing an error "non well formed numeric value encountered". as i am beginner its a big issue for me. expecting your help. thanks in advance.
here is my code
<?php
$date= Session::get("donation_date");
if (isset($date)) {
$today= time();
$difference=($date- $date) > 1;
echo ' '.$difference.' you are capable to donate blood';
}
?>
Upvotes: 0
Views: 31
Reputation: 23958
You are using Unix time on one hand and a string on the other.
Unix time is a sends counter and has a value as integer.
The date is (I assume) something like 2019-03-17, a string.
You can use strtotime to parse the date to Unix, that way you have two integers that can be subtracted.
The result is the difference in seconds, meaning 86400 is a day (disregarding DST).
$date= Session::get("donation_date");
if (isset($date)) {
$today = time();
$difference = $today - strtotime($date);
if ($difference > 864000) echo ' '.$difference.' you are capable to donate blood';
}
Edit: for strtotime to be able to parse the date then it must be a parsable format.
http://php.net/manual/en/datetime.formats.php
If it's not, then just say so and I will help you parse the date.
Upvotes: 1
Reputation: 4557
Looks like you have a typo:
$difference=($date-$date) > 1;
should be:
$difference=($today-$date) > 1;
Also, it would probably be best to ensure that $date
is of the proper format, update:
if (isset($date)) {...}
to:
if (is_numeric($date)) {...}
as $date
is always set as it is assigned directly above.
Hope that helps,
Upvotes: 0