How to Check the Date If the Condition Required Is Greater Than and Less Than the Current Date in PHP

How to check the date if the condition required is greater than and less than the current date in php?

@Arun I didnt Understand your requirement properly But as far i understand you can try like this.

<?php

function s2t($value){
return strtotime($value);
}

$date1 =
$date2 = date('Y-m-d',strtotime('+30days'));
$date3 = date('Y-m-d',strtotime('-30days'));

if( s2t($date1) > s2t($date2) ){
echo "30+ Days left";
}else{
echo "30 Days Left";
}

PHP date time greater than today

You are not comparing dates. You are comparing strings. In the world of string comparisons, 09/17/2015 > 01/02/2016 because 09 > 01. You need to either put your date in a comparable string format or compare DateTime objects which are comparable.

<?php
$date_now = date("Y-m-d"); // this format is string comparable

if ($date_now > '2016-01-02') {
echo 'greater than';
}else{
echo 'Less than';
}

Demo

Or

<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");

if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}

Demo

PHP: Date larger than current date

Try converting them both to timestamps first, and then compare two converted value:

$curdate=strtotime('22-02-2011');
$mydate=strtotime('10-10-2011');

if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}

This converts them to the number of seconds since January 1, 1970, so your comparison should work.

Find if date is less than 7 days from now

Just convert both units to unix timestamp, make your subtraction, then divide it by 86400:

$expiry_date = '2016-04-18';
$today = time();
$interval = strtotime($expiry_date) - $today;
$days = floor($interval / 86400); // 1 day
if($days < 7) {
echo 'less';
}

Or another way with DateTime classes:

$expiry_date = '2016-04-18';
$expiry_date = new DateTime($expiry_date);
$today = new DateTime();
$interval = $today->diff($expiry_date);
$day = $interval->format('%r%a');
if($day < 7) {
echo 'less';
}

Example conditions:

$expiry_date = '2016-04-18';
$today = time();
$interval = strtotime($expiry_date) - $today;
$day = floor($interval / 86400); // 1 day
if($day >= 1 && $day < 7) {
echo 'between 1 - 7 days';
} elseif($day <= 0) {
echo 'deadline';
} else {
echo 'soon';
}

Just change / tweak it depending on what you're trying to do.

If event date is less than now, show foo. If event date is greater than now, show bar

now() is mysql function . In PHP we use time() to get the current timestamp

It is used as

$now = time();// get current timestamp

Use strtotime() to convert date in time stamp then use for comparison

$now = time();
foreach ($events->results() as $e):
if (strtotime($e->event_date) >= $now) {// check for greater then equal to
?>
<h1>Show Event</h1>
<?php
} else {
echo "<h1>DON'T Show Event</h1>";
}
endforeach;

PHP check if date between two dates

Edit: use <= or >= to count today's date.

This is the right answer for your code. Just use the strtotime() php function.

$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));

if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}


Related Topics



Leave a reply



Submit