$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";
}
Related
I have this code:
$workStart = $point->start; // return 01:05:00
$workEnd = $point->finish; // return 05:30:00
$timeNow = Carbon::now()->format('H:i'); // return 12:30:34
if (.....){
echo "user work";
} else {
echo "user NOT work";
}
How can I check if the user is working at the moment or not?
We can compare our current time lies in given format by comparing the current time with start and end time.
$workStart = $point->start; // return 01:05:00
$workEnd = $point->finish; // return 05:30:00
$timeNow = Carbon::now()->format('H:i'); // return 12:30:34
if (strtotime($workStart) < strtotime($timeNow) && strtotime($workEnd) > strtotime($timeNow)){
echo "user work";
} else {
echo "user NOT work";
}
Have you read the documentation for the Carbon date class? There is a between function built in for this.
It makes nice, human readable code:
$workStart = Carbon::createFromTimeString($point->start);
$workEnd =Carbon::createFromTimeString($point->finish);
if (Carbon::now()->between($workStart,$workEnd){
echo "user work";
} else {
echo "user NOT work";
}
try this. it checks based on am/pm. remember to use Carbon\carbon;
public function Checkwork()
{
$meeting = Carbon::now();
$tim=$meeting->format('H:i:s');
$workStart = '01:05:00'; // return 01:05:00
$workEnd = '05:30:00'; // return 05:30:00
if(($tim<$workStart)&&($tim>$workEnd)){
dd('working');
// dd($tim);
}
else{
//dd($tim);
dd('not working');
}
}
You can apply logic as below:
UPDATE: The value fetched from a database needs to be converted from English textual date-time to a UNIX timestamp.
if(strtotime($workStart) < strtotime($workEnd)) {
if ((strtotime($timeNow) > strtotime($workStart)) && (strtotime($workEnd) > strtotime($timeNow))){
echo "user work";
} else {
echo "user NOT work";
}
}
Hope this helps!
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"
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';
}
So I'd like to add the current time to the database, to a specific user when he does something, and later on read it, and check if that time has passed (by checking current time and substracting that from the one in database; to check if it has passed or not)
So how would I do this? I tried with something like this:
$date = date("YmWjis");
$calculate = $date - $info['lastvisit'];
if($calculate <= -1)
{
echo "you need to wait before visiting again"; // (just an example)
} else {
//do something
}
I also tried both:
!$calculate < 0
$calculate < 0
etc. But I can't get it to work. Can anyone help me? :P
edit for Parag;
$date = date("YmWjis");
$dote = date("YmWjis") + $time; // ($time is set earlier and is 30 seconds)
echo "wait " . $date = $date - $dote . " seconds until next visit";
work?
It says like "wait 20138269786674 seconds until next visit".
You can try something like this:
$dateDiff = new DateTime("2014-04-27 22:00:15");
$date = new DateTime();
$diff = $date->diff($dateDiff);
if($diff->invert == 0)
{
echo "you need to wait before visiting again"; // (just an example)
} else {
//do something
}
$db_time = "2014/04/28 15:15:15";
$cur_time = "2014/04/28 18:15:15";
if(strtotime($cur_time) > strtotime($db_time))
{
// Current time exceeds DB time
$diff = date('Y/m/d H:i:s', strtotime($cur_time)-strtotime($db_time));
echo $diff;
}
else
{
// Current time didn't exceeds DB time
}
UPDATE
$date = strtotime(date("YmWjis"));
$dote = strtotime(date("YmWjis")) + $time; // ($time is set earlier and is 30 seconds)
echo "wait " . $date = $date - $dote . " seconds until next visit";
DEMO
http://3v4l.org/LBIXu
Don't use a database. This does the job without the db overhead. It uses PHP sessions.
<?php
session_start();
if (!isset($_SESSION['lastVisitTime'])) {
$_SESSION['lastVisitTime']=new DateTime();
} else {
$now=new DateTime();
if ($_SESSION['lastVisitTime']->diff($now) > $someMaxValueYouDefine) {
echo "You must wait before visiting again.";
}
}
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 />";
}