This question already has answers here:
How can I check if the current date/time is past a set date/time?
(4 answers)
Closed 9 years ago.
How can I check if the date is later than the current date?
if(date("d") < "18" && date("m") < "01") {
echo 'To late!';
}
Doesn't work for me.
You can do this notation:
if( '20140505' > date("Ymd") ) {
// today's date is before 20140505 (May 5, 2014)
}
$time1 = strtotime(date("d/m/Y", "18/01/2014"));
if(time() > $time1)
{
echo "too late!";
}
The best is to use strtotime.
$current_date = date("Y-m-d");
$date_to_compare = date("Y-m-d",time()+86400); //1 day later
if (strtotime($date_to_compare) > strtotime($current_date)) {
echo "too late";
}
Related
This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 4 years ago.
I have to check expiry date is passed with current date and then disable the link. Should i convert strtotime function and check or is there any simple way to check it. We should not compare the dates
$current_date = "2018-07-23 12:00:00";
$expiry date = "2018-07-22 12:00:00";
if($expiry date>$current_date){
$link = "<a href="http://www.example.com";
} else {
$link = "#";
}
I would use strtotime() as follows:
$current_date = "2018-07-23 12:00:00";
$expiry date = "2018-07-22 12:00:00";
if (strtotime($expiry date) > strtotime($current_date)) {
$link = "<a href="http://www.example.com";
} else {
$link = "#";
}
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I have been looking through various posts on here to find my solution but i can't find one :(
so what i am after is a way of finding the exact difference between to dates. I currently have the following code
$date = time();
$publishedDate = strtotime($item->published_time);
$datediff = $date - $publishedDate;
$daysListed = floor($datediff/(60*60*24));
if($daysListed >= 2)
{
$days = $daysListed." days ago";
}
elseif ($daysListed == 1)
{
$days = $daysListed." yesterday";
}
elseif ($daysListed < 1)
{
$days = "today";
}
which does a pretty good job, the main flaw i have with this is say my user submits a post at 23:59pm last night the above code will return a value of "today" because $datediff is returning less than 24 hours, even though it was posted yesterday. Is there a more accurate way of getting the correct value returned?
Appreciate your help
Luke
You need to compare also the time
e.g. $timePub 23:59 $nowtime 11:20
....
elseif ($daysListed < 1)
{
if ($timePub > $nowtime) {
$days = " yesterday";
}
else {
$days = "today";
}
}
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I want to echo how much days are left between a variable and NOW. This is the code
$now = date('Y-m-d');
$daysleft= strtotime($starttheproject -$now);
echo
"Now =".$now.
"<br> Starttheproject =".$starttheproject.
"<br> Daysleft =".$daysleft;
Result of echo:
Row =2014-10-17
Starttheproject =2014-10-22
Daysleft =
Question:
How can calculate the number of days, so that the result will be '5'
I've been playing with code like this, with no luck:
$daysleft= date('Y-m-d', strtotime($starttheproject -$now));
Try with this:
$date = new DateTime('2014-03-10');
$finish = new DateTime();
$difference = $date->diff($finish);
$difference =$difference->format('%R%a');
if ($difference > 10) { //if the post is over 10 days old
echo 'Renew now';
}else{
echo 'Renew in '.$difference.' days</font>';
}
This question already has answers here:
Validate if age is over 18 years old
(7 answers)
Closed 8 years ago.
I've following code to check whether the user is born before today's date(the user date of birth is stored in a variable $form_data['dob']). The code for it is as below:
if(change_date_format_to_db($form_data['dob'], 'Y-m-d') > change_date_format_to_db(date('Y-m-d'), 'Y-m-d'))
$this->mValidator->push_error($errors_msgs['user_dob_greater'], 'dob');
function change_date_format_to_db( $date, $current_format, $seperator = "/" ) {
if ( empty($date) || empty($current_format) ) {
return "";
}
$current_format = strtoupper($current_format);
$format_array = explode("-", $current_format);
$date_values = explode($seperator, $date);
for ($i=0;$i<3;$i++) {
$date_data[ $format_array[$i] ] = $date_values[$i] ;
}
$mysql_date_format = $date_data["Y"]."-".$date_data["M"]."-".$date_data["D"];
return $mysql_date_format;
}
Now instead of checking the user's date before today's date I want the user should be minimum 18 years old on today's date. How should I do this with above code? Thanks in advance.
Keep it simple with DateTime Object
$birthday = DateTime::createFromFormat('Y/m/d', $form_data['dob']);
$diff = $birthday->diff(new DateTime());
// ^^ now
if ($diff->y < 18) {
echo 'this site requires +18yrs';
}
This question already has answers here:
How can I check if the current date/time is past a set date/time?
(4 answers)
Closed 9 years ago.
My date is being returned like 01/05/2013 12:44. How do I check in php if this datetime is in the future?
Try this:
<?php
$dt = '01/05/2013 12:44';
$nowdt = time();
$diff = strtotime($dt) - $nowdt;
echo $diff;
if($diff > 0){
echo (" your date is future date");
} else {
echo ("your date is is not future date");
}
?>
You can compare time() with the UNIX timestamp representation of that date:
if (time() < strtotime("01/05/2013 12:44"))
{
// your time is greater than todays date
}
Another way
$time = strtotime(str_replace("/","-",$date) )
if ($time > time())
{
echo "$date is in the future."
}
From the PHP manual use mktime.
Example in pseudo code:
$today = mktime("today");
$your_date = mktime("...");
if ($your_date > $today) {
// it's in the future
}
Convert to Unix time, then see if it's greater than the current time.
<?php
if(strtotime("01/05/2013 12:44") > time()) {
//time is in the future.
}else{
//time is in the past
}
?>