I'm want to know which date is the greater date
<?php
$date1=16/05/19;
$date2=19/04/19;
if ($date1 > $date2) {
echo 'date1 greater than date2';
else {
echo 'Less than';
}
Why do I get "Less than"?
<?php
$date1=new DateTime("16-05-2019");
$date2=new DateTime("19-04-2019");
if ($date1 > $date2) {
echo 'date1 greater than date2';
} else {
echo 'Less than';
}
?>
Here you compare two date objects (see https://www.php.net/manual/en/function.date.php)
Thanks fo you all guys!
now I have new problem :
$last_update=0;
foreach($datetimetextresult as $value1){
$datetime_text = date("d-m-Y", strtotime($value1->datetime));
$date1=new DateTime ($datetime_text);
$text = $value1->text;
if ($last_update <$date1){
$last_update=$date1;
$last_text = $text ;
}
}
I get this eror: "Object of class DateTime could not be converted to int"
Related
I am attempting to compare two dates to see if it is in the past or future
But even thought the "DateToCheck" is in the past, it always returns "in the future". If the date is set for the future, it also returns in the future.
I used this SO question as a primer to check.
<?php
date_default_timezone_set( 'UTC' );
define( "TIMESTAMP_FORMAT", "Y-m-d G:i:s" );
$aString = "2017-03-01 23:11:16";
echo "String to convert: ". $aString ."\r\n";
$currentTime = date( TIMESTAMP_FORMAT );
echo "Current Time: ". $currentTime."\r\n";
$dateToCheck = new DateTime($aString);
echo "Date To Check: ". $dateToCheck->format(TIMESTAMP_FORMAT)."\r\n";
if($dateToCheck < $currentTime) {
echo 'Date is in the past';
} else {
echo 'Date is in the future';
}
Make sure your server date is correct, then you can use:
if (new DateTime() > new DateTime("2017-03-01 23:11:16")) {
# date is in the past
}else{
# date is in the future
}
Try use epoch seconds:
if($dateToCheck->format('U') < $currentTime->format('U')) {
echo 'Date is in the past';
} else {
echo 'Date is in the future';
}
$current_season_expiry value is "2016-07-23", though the date is not passed yet, regardless of any date it shows "expired". I am comparing $current_season_expiry with today's date.
echo $current_season_expiry;
if( $current_season_expiry >= date("Ymd") ) {
echo "is not expired";
}else{
echo "expired";
}
Test with timestamp :
$timestamp_current_season_expiry = strtotime('2016-07-23');
$timestamp_date = strtotime(date('Y-m-d'));
if( $timestamp_current_season_expiry >= $timestamp_date ) {
echo "is not expired";
}else{
echo "expired";
}
For starters, you need to add dashes in your call to date():
Change:
date("Ymd")
To:
date("Y-m-d")
The original would yield a string of "20160624" instead of "2016-06-24", which would throw off any comparisons.
There's also the future possibility of timezone issues, which you could mitigate by making sure all comparisons are done using UTC:
echo $current_season_expiry_utc; // Make sure this is in UTC
if( $current_season_expiry_utc >= gmDate("Y-m-d") ) {
echo "is not expired";
}else{
echo "expired";
}
... or using timestamps:
echo $current_season_expiry_stamp; // Make sure this is a timestamp
if( $current_season_expiry_stamp >= time() ) {
echo "is not expired";
}else{
echo "expired";
}
I have a problem comparing a customized php datetime with current datetime, the code is below:
$requestDate = new DateTime("2014-05-28 11:14:00");
$nowDate = new DateTime();
$aklTimeZone = new DateTimeZone("Pacific/Auckland");
$requestDate->setTimezone($aklTimeZone);
$nowDate->setTimezone($aklTimeZone);
echo $nowDate->format("Y-m-d H:i:s");
if ($requestDate > $nowDate) {
echo '1';
} else if($requestDate == $nowDate){
echo '0';
} else{
echo "-1";
}
The printed result is:
2014-05-28 12:21:31
1
The $requestDate is supposed to be less than the $nowDate, how come it is greater here?
Thank you
I can't understand what's wrong whit this code I wrote, and why it has an unintuitive behavior
if(($datav == 0) || ((strtotime($datav)) > (strtotime('01/01/2014')))) {
echo 'yes';
}
else if((strtotime($datav)) < (strtotime('01/01/2014'))) {
echo 'no';
}
$datav is a date variable which can or can't be set in a wordpress form I wrote.
here is what's happening: if the date is not set (== 0) the code works, it echoes 'yes'; if the date is set and is before 01/01/2014 it also works, it echoes 'no'; but if the date is set and is after 01/01/2014 it doesn't work and echoes 'no'.
in the third case, I'm sure I've set the right date (a date after 01/01/2014) because I echoed it to check it.
What am I doing wrong?
Thanks anyone.
<?php
$datav = 0;
test('2014-01-01');
test('2015-01-01');
test('01/01/2013');
test('25/12/2014'); // fail because strtotime will resolve as 1970-01-01
function test($datav) {
echo "Input Date: $datav = ";
$timestamp = strtotime($datav);
if ($datav == 0 || $timestamp > strtotime('01/01/2014')) {
echo 'yes';
} else if ($timestamp < strtotime('01/01/2014')) {
echo 'no';
} else {
echo 'neither';
}
echo " - What strtotime thinks was input (".date('d-M-Y', $timestamp).")<br />";
}
I currently have a registration form which checks a persons date of birth via three inputs
day(dd) - month(mm) - year(yyyy)
The code I am using to check:
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
return $end_date - $start_date;
}
//Enter date of birth below in MM/DD/YYYY
$dob="$day/$month/$year";
$dob2 = "$dob";
$one = round(dateDiff("/", date("d/m/Y", time()), $dob2)/365, 0) . " years.";
if($one <13){
?>
You must be 13 years of age or older to join!
<?
}else{
?>
YAY you're 13 or above!
<? } ?>
I am receiving an error saying:
error: Warning: gregoriantojd() expects paramater 1 to be long string
Can anyone help me with this?
Thank you in advance!
Why complicate when you can do it in really simple way:
function dateDiff($dateFormat, $beginDate, $endDate)
{
$begin = DateTime::createFromFormat($dateFormat, $beginDate);
$end = DateTime::createFromFormat($dateFormat, $endDate);
$interval = $begin->diff($end);
if($interval->y >= 13)
{
echo 'Over 13';
}
else
{
echo 'Not 13';
}
}