Can date() be compared like this? [duplicate] - php

This question already has answers here:
Trying to define my own time type in PHP
(2 answers)
Closed 6 years ago.
I have had some issues with comparing date/times.
I had a working code that just stopped working all of a sudden. Now I made some changes to it and by some reason UNIX time is not correct.
Utime and nexttime is just an example string now, but in the code Utime is last time updated, and nexttime is next time a update will happen calculated from Utime.
Example:
$Utime = "201603300450"; // YmdHi 2016 03 30 04:50
$nexttime = "201603300520";
if (date("YmdHi")>=$Utime && date("YmdHi")>=$nexttime)
Is this a safe way to compare the timevalue? I had some issues with strtotime thats why I ask.

You should convert your dates to DateTime objects:
<?php
$Utime = "201603300450";
$nexttime = "201603300520";
$dateUtime = DateTime::createFromFormat('YmdHi', $Utime);
$datenexttime = DateTime::createFromFormat('YmdHi', $nexttime);
$nowDate = DateTime::createFromFormat('YmdHi', date('YmdHi'));
if ($nowDate>=$dateUtime && $nowDate>=$datenexttime)
//do your stuff...

Strtotime should work just fine, but you should stick to a valid format. "Y-m-d H:i:s" is a valid format for strtotime.
Hope it helps :)

strtotime() is a great helper in this regard.
Check out strtotime
$Utime = "201603300450";
$nexttime = "201603300520";
if ( strtotime('now') >= strtotime($Utime) && strtotime('now') >= strtotime($nexttime) )

Related

Calculating time passed since a textual date/time value [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I have a database from which I extract this date/time value: 2018-01-19 09:50:54
I want to print how many hours and minutes passed since that date, with regard to current server time:
I try this code:
$prev_date = "2018-01-19 09:50:54" // extracted from DB
$date_now = date("Y-m-d H:i:s"); // 24-hour date-time, matches DB format
$interval = $prev_date->diff($date_now); // I saw this on another thread
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
I get:
Fatal error: Call to a member function diff() on a non-object (on the
$interval=.... line )
I guess it is some kind of formatting problem, how do I fix that?
$prev_date is not a object. You need to transform to a DateTime object to use diff. Also, date() will return a string, you must use a DateTime object for this to be able to use diff function.
$prev_date = DateTime::createFromFormat('Y-m-d H:i:s', '2018-01-19 09:50:54');
$date_now = new DateTime(); // 24-hour date-time, matches DB format
$interval = $prev_date->diff($date_now); // I saw this on another thread
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";

DateTime comparison not working PHP [duplicate]

This question already has answers here:
How to compare two time in PHP
(11 answers)
Closed 6 years ago.
timing comparison not working out if passed dynamically
$date = new DateTime();
$date->setTimezone(new DateTimeZone('America/New_York'));
$myTime =$date->format('H:i');
$myDay =$date->format('l');
if(($s<=$myTime) && ($e>=$myTime||$e=="00:00"))
{$open =1;}
Following works if hard coded
if(("11:30"<="05:39")&&("23:00">="05:39"||"23:00"=="00:00"))
{$open =1;}
where m i going wrong
// your first date
$dateA = '2008-03-01 13:34';
// your second date
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// something here
}
first of all you are using strings to compare time. Convert to timesstamp using strtotime (http://php.net/manual/en/function.strtotime.php) or mktime(http://php.net/manual/en/function.mktime.php) or use DateTime class (http://php.net/manual/en/class.datetime.php). When comparing strings they are not compared as time, but as character sequences.

Weeks between 2 mysql formatted datetime [duplicate]

This question already has answers here:
MySQL week calculation between two dates
(2 answers)
Closed 10 years ago.
I am creating a Top 30 Music Chart..
Hw do I get the number of weeks between two mysql formatted datetimes? like
From: 2013-01-15 11:41:14
Current Datetime: 2013-02-25 13:41:14
Using SQL is better, but in php it'll be something like this:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
Subtract the times using strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
You can just chunk down the seconds with "remainder" and find out the minutes (and it's remaining seconds) and so on until you hit the time-distance you want.
does the WEEK() function helps you in your query ?
I don't know Mysql very well but lets assume the following :
select WEEK(date1-date2) from mytable where name='Ken';
You can use strtotime() method, then count difference between current time and time you have in your MySQL database.
Try DATEDIFF(date1, date2) function in mysql.
DATEDIFF() will return you the no of date between two given date. Hope you can easily get the weeks from output.

Time comparison in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
strtotime date weird result
I am trying to compare two times for an attendance system, i am trying to get the accurate time for Arkansas,USA but im not being to able to get the correct time. Please, guide me in the right direction if i am doing something wrong, part of my code:
$curTime =DATE("H:i A", strtotime('GMT-6'));
foreach($res as $r)
{
$crs_id =$r['course_id'];
$query1 = "select * from tbl_course_time where course_id='$crs_id'";
$res1 = $obj->querySelect($query1);
$start_time = DATE("H:i", STRTOTIME($res1[0]['start_time']));
$end_time = DATE("H:i", STRTOTIME($res1[0]['end_time']));
if ($curTime > $start_time && $curTime < $end_time)
{
$qry="insert into tbl_attendance set stud_id='$id',attd_date='$curDateForDB',attd_time='$curTimeForDB',course_id='$attd_courseId'";
$res=$obj->queryInsert($qry);
$result1 = "taken";
}
}
It's giving me incorrect time's is it the GMT time im getting wrong or something else
Any help would be greatly appreciated, Thank you
Don't use strtotime('GMT-6') because GMT-6 is not a time; it's a time zone.
// gets the UTC time using gmdate() instead of date()
$utc_str = gmdate("M d Y H:i:s", time());
// get the UTC timestamp, should you need it
$utc = strtotime($utc_str);
If you're in PHP 5, the DateTime class is a very clean way to get times and also deal with time zone conversions.
Also, I encourage you to set your database up using UTC time, and convert the output to the user's time zone.
You're doing the gmt-6 business wrong. The following is on a server that's in GMT-6 already:
echo date('r'); // Thu, 13 Dec 2012 02:40:00 -0600
echo date('r', strtotime('gmt-6')); // Thu, 13 Dec 2012 08:40:17 -0600
Note the 6 hour difference (and 17 seconds while I was typing up the strtotime bits). Doing the gmt-6 bit in strtotime forces PHP to apply a 6 hour difference to a string that's already in some timezone already.

Calculating the elapsed time [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determining elapsed time
I want to know how I can calculate the elapsed time beween 2 dates I have in a DB.
They are saved as 'EntryDate' and 'ExitDate' in date/time as DD-MM-YYYY 00:00:00 and I want to calculate the time between two for instance:
01.01.2012 12.00.00 and 01.01.2012 13.30.00 which would be 1hr 30 mins.
Thanks. Sam.
You probably want to use timediff, when working inside mysql.
mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
-> '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'
For the implementation in PHP you need to use date_diff function.
$d_start = new DateTime($EntryDate);
$d_end = new DateTime($ExitDate);
$diff = $d_start->diff($d_end);
// return all data
$this->year = $diff->format('%y');
$this->month = $diff->format('%m');
$this->day = $diff->format('%d');
$this->hour = $diff->format('%h');
$this->min = $diff->format('%i');
$this->sec = $diff->format('%s');
Use this forum thread: http://forums.devshed.com/php-development-5/calculate-remaining-day-minute-seconds-between-two-dates-538979.html
Next time google a little before posting. We will help you but you shouldn't abuse...
Because the dates are formatted like this, you can use strtotime to get the timestamps for each date. Then you just subtract the exit from the entry. This leaves you with the difference in seconds. Now you can play around with this.
you can use
eg:
select TIMEDIFF ('2006-10-31 11:50:31' , '2006-10-31 11:50:01')
SELECT TIMESTAMPDIFF(MINUTE,'2012-01-01 12:00:00','2012-01-01 13:30:00')
This will return the difference in minutes. You can replace Minute with any time unit (like 'HOUR', 'SECOND', etc.)

Categories