Weird behaviour of strotime comparison - php

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 />";
}

Related

Checking if the user is working at a given time in php

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!

Not get the real greater date

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"

PHP Date Always show expired

$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";
}

Validation Error dates PHP

I'm trying to validate dates in PHP, but the problem is that with some dates work, an example would be: "02/2/2015" returns true, "20/12/2015" false returns, is a serious problem and I see no error in the code.
Function.
<?php
function check_date($date) {
$open_date = explode('/', $date);
if (count($open_date) == 3) {
if (checkdate($open_date[0], $open_date[1], $open_date[2])) {
return true;
} else {
return false;
}
} else {
return false;
}
}
//$date = "02/2/2015"; // return true !
$date = "20/12/2015"; // return false ?
if(check_date($date)) {
echo "valid";
} else {
echo "invalid";
}
?>
How could solve this problem?
checkdate expects a month, day and year, in that order:
https://secure.php.net/manual/en/function.checkdate.php
If your dates are formatted as day/month/year then you can still use checkdate, you'll just have to change the order of the parameters:
if (checkdate($open_date[1], $open_date[0], $open_date[2]))
The signature of checkdate function looks like checkdate(month,day,year); . You can have upto 12 months and not 20. :-)

Fix time string

This should be very easy, but I am not getting it. I wish to receive hours:minutes, and return hours:minutes:seconds in 00:00:00 format. The following falls way short. Recommendations on the best way to do this? Thank you
<?php
function fixTime($t)
{
$a = explode(':', trim($t));
return ((count($a)==2) && ($h=$a[0]) && ($m=$a[1]) && ($h>=0) && ($m>=0) && ($h<=23) && ($m<=59))?$h.':'.$m.':00':'00:00:00';
}
echo(fixTime('23:33').'<br />');
echo(fixTime('05:00').'<br />');
echo(fixTime('5:00').'<br />');
?>
The strtotime function will help you out here:
function fixTime($t) {
return date('H:i:s', strtotime($t));
}
echo fixTime('23:33'); // 23:33:00
echo fixTime('05:00'); // 05:00:00
echo fixTime('5:00'); // 05:00:00
Also, see the date function for a list of formatting options available.
To validate such simple input, a regular expression might be helpful:
function fixTime($t) {
if (!preg_match('/^([01][0-9]|2[0-3]|[0-9]):[0-5][0-9]$/', $t)) {
return false;
}
return date('H:i:s', strtotime($t));
}
echo fixTime('23:33'); // 23:33:00
echo fixTime('33:33'); // false
if (fixTime('33:33') === false) {
// invalid date supplied
}

Categories