Welcome Guest, Not a member yet? Register   Sign In
time comparing problem
#1

I am trying to compare two times on the same day.
Code:
$myTime = Time::now('Asia/Kolkata', 'en_US'); // get the current date and time

echo $currentTime = Time::parse($myTime); // this prints the current date and time with seconds
echo $openTime = Time::parse('15:00:00', 'Asia/Kolkata'); // this prints the current date with provided time (15:00:00)

now when I compare 
if($currentTime->isBefore($openTime)){
//This does not work
}
else calls


Code:
if i remove local
$openTime = Time::parse('15:00:00'); // this also prints the current dare with provided time (15:00:00)

but this time the statement inside the if block called
if($currentTime->isBefore($openTime))
{
//this called
}

one more problem is that even after midnight today, means after 12:00:00 AM
the if block works which is not supposed to work because the date is changed.
Reply
#2

PHP DateTime: Create, Compare and Format Dates Easily
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(05-08-2023, 10:00 PM)InsiteFX Wrote: PHP DateTime: Create, Compare and Format Dates Easily

Do you mean I need to use other approaches than the one in the CI guide?
Reply
#4

It is not known what the "current" time is. Maybe it's more than 15:00:00
time::parse(); does not convert time to time zone.
Reply
#5

The problem was with Time::parse, if we don't pass the locale it will automatically convert the time to the default locale
so no matter where you convert the time while parsing you need to pass the locale.
for ex.


Code:
$myTime = Time::now('Asia/Kolkata', 'en_US'); // get the current date and time

echo $currentTime = Time::parse($myTime, 'Asia/Kolkata');
echo $openTime = Time::parse('15:00:00', 'Asia/Kolkata');
Reply
#6

Time::parse() takes a string as a first parameter.
https://codeigniter4.github.io/CodeIgnit...e.html#now

The string value for the time object does not have timezone.
So the default timezone is used if you don't specify a timezone as the second parameter of Time::parse().

By the way, why do you need to parse the Time object?
It has already the current time.
Reply
#7

What I use and works:

PHP Code:
  $now date('Y-m-d H:i:s');
  $datetime1 = new \DateTime($now);//start time
  $datetime2 = new \DateTime($maybe_from_your_database_column);//end time
  $interval $datetime1->diff($datetime2);
  $result $interval->format('%i');//00 years 0 months 0 days 08 hours 0 minutes 0 seconds 


I get minutes as result
Reply
#8

(05-13-2023, 07:40 PM)kenjis Wrote: Time::parse() takes a string as a first parameter.
https://codeigniter4.github.io/CodeIgnit...e.html#now

The string value for the time object does not have timezone.
So the default timezone is used if you don't specify a timezone as the second parameter of Time::parse().

By the way, why do you need to parse the Time object?
It has already the current time.

as suggested by your user guide, I parse time so I can compare two times.

Code:
<?php

$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago');
$time2 = Time::parse('January 11, 2017 03:50:00', 'America/Chicago');

$time1->isBefore($time2); // true
$time2->isBefore($time1); // false

https://www.codeigniter.com/user_guide/l...-two-times
Reply




Theme © iAndrew 2016 - Forum software by © MyBB