Comparing time in PHP - php

I am comparing a deadline time from my database which is on a 24-hour format (like 3:00 = 15:00) to the time now.
For example, if a student wasn't able to submit on the due time, an upload link will be greyed out.
I'm okay with the date comparison, my problem is the time.
Here's my current code:
$date = date("Y-m-d");
$time = strtotime(date('G:i:s'));
$deadtime = strtotime($r['upload_deadtime']);
/*------$deadtime = 15:00:00 ---------*/
if(date($r['upload_deadline']) >= $date && $deadtime > $time){
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
Update: let's just forget about the date comparison, what I'm trying to say is how can I compare my deadline time (which is on a 24-hour format (like 3:00:00 = 15:00:00)) to the time now.
Here's my current code:
$time = strtotime(date('G:i:s')); /*which returns 23:15:42 instead of 14:15:42 */
$deadtime = strtotime('15:00:00');
if($deadtime > $time){
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}

date_default_timezone_set('Asia/Manila');
$deadtime = strtotime("15:00:00");
if ($deadtime - time() > 0){
echo '<td>upload</td>';
}else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
This may solve your time comparison issue!!
strtotime("15:00:00") will convert time into unix timestamp of today's 15:00:00 and time() have current unix timestamp.

You just have the comparison backwards on your time check.
You are checking if the deadline date is in the future (assuming the script always runs in the present) and then you are checking that the deadline time is less than the current time (IE before), on a date time epoch value.
With the check you are doing on the strtotime, you do not need to check the dates, as they are already included with the date and time by means of epoch.
Simply do:
if($deadtime > $time){
echo '<td>upload</td>';
} else {
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}
This will only show the upload link in the table if the deadline time is in the future.
EDIT:
In the case of your new code, you just need to make the deadtime a strtotime of the deadline date and time stamp from your dBase. Then the code will work.
You are still also comparing the wrong way around!
Example:
/* deadtime = strtotime of datetimestamp in format yyyy-mm-dd G:i:s */
/* $time = strtotime of now. */
if($deadtime > $time){
echo '<td>upload</td>';
} else {
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}

Use time PHP's DateTime::Diff
// Current Date and Time
$date_time_now = new DateTime();
// Date Time values from DB (Assuming it's coming from database)
$date_time_deadline = new DateTime('2014-09-08 15:00:00');
// Calculate difference
$difference = $date_time_now->diff($date_time_deadline);
// Output difference in human-readable format
echo "Difference: " . $difference->format('%d days, %h hours, %i minutes, %s seconds');
http://php.net/manual/en/datetime.diff.php

Here's my working code. I think you'll know that my point is.
date_default_timezone_set('Asia/Manila');
$time = date('H:i:s');
$deadline_time = date("h:i a",strtotime($r['upload_deadtime']));
list($hours,$mins,$secs) = explode(":",$time);
$hours = date("H") + 9;/*-gives me wrong hours so i got to modify it-*/
$time_array = array($hours,$mins,$secs);
$new_time =implode(":",$time_array);
$deadtime = strtotime($deadline_time);
$time_now = strtotime($new_time);
if($r['upload_deadline'] < $date && $deadtime < $time_now){/*-compares the date and time-*/
echo '<td>upload</td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}

Related

Converting hours to timestamps

To convert a date to timestamp, I usually do this- strtotime ("2018-05-17 05:04:34) but now, I want to convert just hours (without date) e.g. 02:00:00 to timestamp. How do I do this?
Why I need this is to compare if a certain time is greater than the hour specified. This is what I am doing:
$reported = strtotime("2018-05-17 05:04:34");
$respons = strtotime("2018-05-17 17:04:34);
$response_time = $respons - $reported;
I want to be to check if $response_time is greater than 1 hour.
There is DateTime::diff, which probably does what you need
https://secure.php.net/manual/de/datetime.diff.php
In your case that should be
$datetime1 = new DateTime("2018-05-17 05:04:34");
$datetime2 = new DateTime("2018-05-17 17:04:34);
$interval = $datetime1->diff($datetime2);
echo $interval->format('H hours');
Strtotime has no problem parsing a time without a date.
No need to fake a date which will come back and bite you with daylight savings.
I also added a check to see if the start/end is "reversed".
$start= "2018-05-17 05:04:34";
$end = "2018-05-17 17:04:34";
//Note that it's intentionally reversed
$diff = strtotime(substr($start,11))-strtotime(substr($end,11));
//If the calculation was reversed add one day in seconds
if($diff <0) $diff += 86400;
If($diff >3600){
Echo "more than one hour";
}Else{
Echo "less than one hour";
}
https://3v4l.org/g1jZH
I believe only I have understood your question correctly.
I want to convert just hours (without date) e.g. 02:00:00 to timestamp.
There's no Date component here.
Okay, I assume they are of same date. If that's the case, just append an arbitrary date in front of the two to make the strtotime() function work:
$start = "05:04:34";
$end = "17:04:34";
$reported = strtotime("2018-05-17 " . $start);
$respons = strtotime("2018-05-17 " . $end);
$response_time = $respons - $reported;
if ($response_time > 3600)
echo "More than hour!";
else
echo "Less than hour!";
Note: This doesn't work if the start time is 17:00 and end time is say, 08:00 - which occurs in the next day. You have to make sure if the start time is greater than end time, then you have to add one more day to the end time.
I like the DateTime class, give this a try:
<?php
$reported = new DateTime('2018-05-17 05:04:34');
$reported->modify('+2 hours');
$now = new DateTime();
echo $now < $reported ? 'less than 2 hours' : 'more than 2 hours';
See it here https://3v4l.org/T7BEL
See the DateTime class docs here http://php.net/manual/en/class.datetime.php

Check if 1 timestamp is later then current timestamp

I have a script that counts delivery time... so... timestamp of the orderdate - current timestamp shows a time...
this all works fine. But sometimes there are orderd where the client can set a time or date in the future..
so for those orders i dont want to show the counter but what to do something like this (all timestamps are 0000-00-00 00:00:00)
$time1 = date($orderItem->elapsed_time); //now
$time2 = date($orderItem->orderdate);
if ($orderItem->later_order_date LATER THEN $time1) {
$elapsed = "not started"
}
else {
$elapsed = $elapsedTime->getElapsedTimeOld($time1,$time2);
}
What is the best way to do this ?
DateTime() objects are comparible which makes this easy:
$time1 = new DateTime(); // No need to pass it anything to represent now
$time2 = new DateTimedate($orderItem->orderdate);
if ($time2 > $time1) {
$elapsed = "not started"
}
else {
$elapsed = $elapsedTime->getElapsedTimeOld($time1,$time2);
}
You can use the the strtotime() function which will convert your formatted date from YYYY-mm-dd HH:ii:ss to a unix timestamp, which is just a number you can compare with regular comparison operators:
if (strtotime($orderItem->later_order_date) > strtotime($time1)) {
$elapsed = 'not started';
}

If time now is more than 3 hours since last update

I need to have a validation that checks if the "last_update" is done before 3 hours or less.
I have this PHP code:
if($user['last_update'] < strtotime("3 hours")) {
$msg .= "You can only register every 3 hour.";
}
How could this be done?
If $user['last_update'] is in date format, then
if (time() - strtotime($user['last_update']) < 3 * 3600) {
//..
}
Try like this. Wrap your $user['last_update'] inside strtotime().
if(strtotime($user['last_update']) < strtotime("3 hours")) {
$msg .= "You can only register every 3 hour.";
}
You can use this to get the current time minus 3 hours:
$dateTime = new DateTime();
$dateTime->modify("-3 hours");
$time = $dateTime->format("H:m:s");
$date = $dateTime->format("Y-m-d");
You can then compare the date and time variables with your last update.
strtotime()
returns seconds.
If your date is stored as UNIX timestamp, then your code is correct.
If your date is stored as TIMESTAMP, following will be the code:
$three_hours = date('Y-m-d H:i:s', strtotime('3 hours'));
if ($user['last_update'] < $three_hours) {
$msg .= "You can only register every 3 hour.";
}

Failing to check if time is in the past using strtotime

I am trying to check if a certain time is in the past. Solution 1 works but the solutions 2 and 3 using strtotime do not. Any ideas why the strtotime solutions fail with this date when they work fine when the date is not that distant (f.ex. using 27.05.2035 works)?
<?php
$date = "27.05.2045";
$hour = "22";
$min = "15";
// 1. This one works
$datetime = DateTime::createFromFormat('d.m.Y H:i', $date.' '.$hour.':'.$min);
$now = new DateTime();
if ($datetime < $now)
{
echo "Datetime is in the past";
}
else if ($datetime > $now)
{
echo "Datetime is in the future";
}
// 2. Does not work
if (time() > strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime)";
}
else if (time() < strtotime($date.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime)";
}
// 3. Using another date format but still does not work
$array = explode('.', $date);
$date_converted = $array[2].'-'.$array[1].'-'.$array[0];
if (time() > strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the past (strtotime with converted date)";
}
else if (time() < strtotime($date_converted.' '.$hour.':'.$min))
{
echo "Datetime is in the future (strtotime with converted date)";
}
?>
The 32 bit integer maximum makes it impossible to represent dates past 19 January 2038.
The solution would be to either:
Use DateTime objects, which do not represent dates using the number of seconds passed since 1970, but using a field for each time unit.
Use the 64-bit version of PHP, where the maximum integer is much higher.
See The 2038 Year Problem for more details.

PHP date comparison

How would I check if a date in the format "2008-02-16 12:59:57" is less than 24 hours ago?
if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }
Just adding another answer, using strtotime's relative dates:
$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
// Do something
}
I think this makes the code much more readable.
if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
// less than 24 hours ago
}
e.g. via strtotime and time().
The difference must be less then 86400 (seconds per day).
<?php
echo 'now: ', date('Y-m-d H:i:s'), "\n";
foreach( array('2008-02-16 12:59:57', '2009-12-02 13:00:00', '2009-12-02 20:00:00') as $input ) {
$diff = time()-strtotime($input);
echo $input, ' ', $diff, " ", $diff < 86400 ? '+':'-', "\n";
}
prints
now: 2009-12-03 18:02:29
2008-02-16 12:59:57 56696552 -
2009-12-02 13:00:00 104549 -
2009-12-02 20:00:00 79349 +
only the last test date/time lays less than 24 hours in the past.
Php has a comparison function between two date/time objects, but I don't really like it very much. It can be imprecise.
What I do is use strtotime() to make a unix timestamp out of the date object, then compare it with the output of time().
Just use it.....
if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}
Maybe it will be more easy to understand...
$my_date = '2008-02-16 12:59:57';
$one_day_after = date('Y-m-d H:i:s', strtotime('2008-02-16 12:59:57 +1 days'));
if($my_date < $one_day_after) {
echo $my_date . " is less than 24 hours ago!";
} else {
echo $my_date . " is more than 24 hours ago!";
}
There should be you variable date Like
$date_value = "2013-09-12";
$Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();
You can Compare both date after convert date into time-stamp so :
if(strtotime($current_date) >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
OR
if($current_date_time_stamp >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
You can use Simple PHP to do this:
$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();

Categories