I have two dates here and want to compare with another date ...
another date here is called $date_main and compared it with a variable called $date_to ... Help it's not working..
<?php
$date_f="02-05-2014";
$date_t="02-11-2014";
$date_from=date('d-m-Y', strtotime($date_f));
$date_to=date('d-m-Y', strtotime($date_t));
$date_c="21-7-2014";
$date_main=date('d-m-Y', strtotime($date_c));
if(($date_main<$date_to))
{
echo "date_main is less then to<br/>";
}
?>
You'll have to compare the times and not the dates itself:
if(strtotime($date_c) < strtotime($date_t))
I'm wondering why not doing something like this?
$date_f = "02-05-2014";
$date_t = "02-11-2014";
$date_from = DateTime::createFromFormat('d-m-Y', $date_f);
$date_to = DateTime::createFromFormat('d-m-Y', $date_t);
$date_main=DateTime::createFromFormat('d-m-Y', '21-07-2014');
if (($date_main < $date_to)) {
echo "date_main is less then to<br/>";
}
Working with objects is always better.
$date_f="02-05-2014";
$date_t="02-11-2014";
$date_main=date_create("2014-7-21");
$date_to=date_create("2014-11-02");
$diff=date_diff($date_main,$date_to);
if($diff->format("%R%a")>0)
{
echo "date_to is greater";
}
else
{
echo "date_main is greater";
}
Related
I am new to PHP and would like to compare a specific time with the current time. I have tried different tricks but nothing works for me.
Eg.
$my_time = '2:00';
$active='';
//echo "The time is " . date("h:ia");die;
if (date('h:i') > date('h:i', strtotime($myTime))) {
$active='Dinner';
$dinner=1;
}
else
{
$active='Lunch';
$lunch=1;
}
You have variable definition
$my_time = '2:00'
but you're using
strtotime($myTime))
This might help you
$my_time ="14:08:10";
if (time() >= strtotime($my_time)) {
echo "Dinner";
}else echo "Lunch";
This will compare given time with current time
try this one .
$my_time ="14:08:10";
if (time() >= strtotime($my_time))
{
echo "Dinner";
}
else
{
echo "Lunch";
}
if this doesnt work try to add date in to time.
because adding date will make time to be more specific.
I am new in PHP
when I am trying to do this
if( date('m-Y',strtotime('2016-11-01 00:00:00')) < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
but it always do false [output 'no'].
I must need to compare months is less than current month , means compare date do not have same months
where I am wrong to compare that date ?
Use DateTime to compare dates:
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
I copied your program so that it reads:
<?php
$x=date('m-Y',strtotime('2016-11-01 00:00:00'));
echo "$x\n";
$y=date("m-Y");
echo "$y\n";
if ($x < date('m-Y') ) {
echo "yes";
} else {
echo 'no';
}
On running it the output is:
# php x.php
11-2016
01-2017
no
That is why it fails. If you are checking for just the month you need to check for equality. Otherwise you need to reorder the date formatting to be "Y-m" (not 'm-Y') for less/greater than comparisons. Comparing the strings is fine.
date function always return a string. In your if construct you compare two strings. For current time:
"11-2016" < "01-2017"
In this case "11-2016" greater than "01-2017".
It will be better to use DateTime class.
$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();
if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
echo 'yes';
} else {
echo 'no';
}
or in your example you need to change format to 'Y-m'.
You should use a decent format to compare the dates. Instead of m-Y, use Y-m-d.
Currently, you are converting the dates to strings, with their months first. So the first date becomes 11-2016, the second becomes 01-2017. PHP compares these as strings, and finds that 0 is less thans 1, so considers the second string to be less.
It's will be right ? for use < , > operator for compare date on php ?
I tested it. And it's work good. But i not founded documents for advice about use < , > operator for compare date on php.
Please tell me can we use < , > operator for compare date on php ?
<?PHP
$today = "2010-10-19";
$expired = "2012-12-10";
if($expired > $today)
{
echo "not expired";
}
else
{
echo "expired";
}
?>
As long as your dates are in a comparable format this will work. Since they are compared as strings you need to make sure that the larger dates components are first and the smaller ones last. For example, the year, then the month, then the date. Always include leading zeros. That's what you do in your example so it works.
If you do not have the dates in this format your comparison will fail some of the time. For example:
'2016-07-04' > '2016-01-07' // true
'04-07-2016' > '07-01-2016' // false
The best way to compare dates is with DateTime() as DateTime objects are comparable and also account for all things date related like leap year and DST.
<?PHP
$today = new DateTime("2010-10-19");
$expired = new DateTime("2012-12-10");
if($expired > $today)
{
echo "not expired";
}
else
{
echo "expired";
}
Use strtotime to convert datetime description into a Unix timestamp and you can easily compare timestamp with < or > operator
$today = strtotime('2010-10-19');// return 1287426600
$expired = strtotime('2012-12-10');// return 1355077800
if ($expired > $today) {// easily comparison of 1355077800 > 1287426600
echo "not expired";
} else {
echo "expired";
}
I had visited some of the post retrieving the date difference between 2 dates in SO but it doesn't gave me the answer I was seeking. Same for reading the documentation, I had problem understanding how it works.
I have tried coding it but it doesn't behave like what I was expecting. Here is my code:
<?php
$currentDate = new DateTime();
$createDateJoin = date_create($getDate['date_joined']);
$dateJoin = date_format($createDateJoin, "Y-m-d");
$yearDifference = $currentDate->diff($createDateJoin);
if ($yearDifference->d < 31 && $yearDifference->m = 0 && $yearDifference->y == 0) {
echo $yearDifference->d . " days";
} else if ($yearDifference->m > 3) {
echo $yearDifference->m . " month";
} else if ($yearDifference->y > 1) {
echo $yearDifference->y . " years";
} else {
echo "Not yet assigned";
}
?>
As you can see from my code above, I am trying to do a print when after calculating the difference between the 2 dates, it meets the condition of $yearDifference->.The behavior from the program that I have experienced does not print out the things I want accordingly (E.g Staff working more than 1 year will print out how many years they have work, months for those who just came in and new staff less than a month will print out days).
I would like to know how does ->d/m/y works and how can I actually make use of the d,m and y to draw out the specific date correctly. And I also noticed that when I treat $yearDifference as a String or int, it comes out different result for the conditions. So what should I treat the type to be to manipulate it more easily? Greatly appreciate the help.
You can use this code to get the date different method diff() object returns more values to check you can print_r your object that will print all data member that are returned via diff() method
<?php
echo get_date_diff(strtotime('1990-10-12'),strtotime('2015-10-14'));
function get_date_diff($date,$dateEnd) {
$dStart = new DateTime(date("Y-m-d", $date));
$dEnd = new DateTime(date("Y-m-d", $dateEnd));
$dDiff = $dStart->diff($dEnd);
return($dDiff->y.' years <br>'.$dDiff->m.' months <br>'.$dDiff->d. ' days');
}
?>
How do i compare two dates in php?.
I have the following dates. One is coming from DB. and another is from Datepicker
$dbdate = 01-06-201402-06-201403-04-201405-06-2014 //DB date
$datepickerDate = 06-06-2014 //Datepicker date.
Here $dbdate is in foreach loop and both formats are dd-mm-yyyy. How do i compare a single date from datepicker to the date in $dbdate?
Compare with STRTOTIME function in for loop like this
return STRTOTIME($dbdate) === STRTOTIME($datepickerDate);
best thing is convert all to unix time stamp..
php method is strtotime()
then it return integer value...u can compare that values
http://www.php.net//manual/en/function.strtotime.php
but the thing is ur dbdate parsing more than 1 date at a time..u should get 1 date at a time
This is not an ideal solution - but I hope it helps you out ;-).
<?php
// Input
$dbdate = '01-06-201402-06-201403-04-201405-06-2014';
$datepickerDate = '06-06-2014';
// Please note: it would be possible to check dates directly in this for loop.
// For educational purposes I splitted it.
$datepickerDateTime = strtotime($datepickerDate);
// Transform concatenated dates to separate dates in array.
$datesList = array();
$dateOffset = 10;
for($iCurrentDateOffset = 0; $iCurrentDateOffset < strlen($dbdate); $iCurrentDateOffset += $dateOffset) {
$datesList[] = substr($dbdate, $iCurrentDateOffset, $dateOffset);
}
// Compare each separate date to $datepickerDate
foreach($datesList as $date) {
if(strtotime($date) < $datepickerDateTime) {
echo("$date is before $datepickerDate<br />");
} else if(strtotime($date) == $datepickerDateTime) {
echo("$date is equal to $datepickerDate<br />");
} else if(strtotime($date) > $datepickerDateTime) {
echo("$date is past $datepickerDate<br />");
}
}
// ...
Output is:
01-06-2014 is before 06-06-2014
02-06-2014 is before 06-06-2014
03-04-2014 is before 06-06-2014
05-06-2014 is before 06-06-2014