PHP compare time from SQL database with current time - php

I would like to check if a time in an SQL database is more than a day old.
I am able to read the datetime off the SQL row and get the current time. What I am unable to do, however, is add 1 day/24 hours to the time pulled from the row, and comparing the 2.
$currenttime = date("Y-m-d h:m:s");
$tmstp = $row['datetime'];
$newtime = date("Y-m-d h:m:s", $tmstp);
echo $tmstp."<br>".$currenttime."<br>".$newtime;
returns the following:
Notice: A non well formed numeric value encountered in C:\xampp\htdocs\test.php on line 12
0000-00-00 00:00:00
2016-03-12 09:03:39
1970-01-01 01:01:00
the null time is to be expected, I initialized the row but didn't pass a value to it. Setting it to anything other than null(including setting it to an arbitrary date more than 24 hours ago but after 1970-1-1 OR adding one hour to it:
$currenttime = date("Y-m-d h:m:s");
$tmstp = $row['datetime'] + strtotime('+ 24 hours');
$newtime = date("Y-m-d h:m:s", $tmstp);
echo $tmstp."<br>".$currenttime."<br>".$newtime;
sets it to tomorrow at this time:
1457903185
2016-03-12 10:03:25
2016-03-13 10:03:25
Once I can get the expected values, how would I effectively compare the 2 values?
Thanks for any help

For compare times you can use the strtotime like this:
$tomorrow = strtotime('+ 24 hours');
$timestamp = strtotime($row['datetime']);
if ($timestamp > $tomorrow) {
// The date in database is greater than tomorrow
}

Related

How to create DateTime from a Unix timestamp with milliseconds? [duplicate]

I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
echo date('Y-m-d h:i:s',$item->timestamp);
doesnt work for me and also this
$unix_time = date('Ymdhis', strtotime($datetime ));
and this :
$item = strtotime($txn_row['appoint_date']);
<?php echo date("Y-m-d H:i:s", $time); ?>
what should i use?
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date function:
echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54
A 13 digit timestamp is used in JavaScript to represent time in milliseconds. In PHP 10 a digit timestamp is used to represent time in seconds. So divide by 1000 and round off to get 10 digits.
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
You can achieve this with DateTime::createFromFormat.
Because you've a timestamp with 13 digits, you'll have to divide it by 1000, in order to use it with DateTime, i.e.:
$ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s");
echo $date;
//2015-10-03 06:00:54
DEMO
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezone to avoid any warnings
date_default_timezone_set('Europe/Lisbon');
NOTE:
More about php date and time at php the right way

How to get next time occurrence from now, submitted in php?

I have an input file with type time. what I want to do is to get time from the moment now that is if time right now is 2019-11-26 23:50:00 and the value of input field if 22:30:00, I should get a date 2019-11-27 22:30:00.
How can achieve this in PHP? in short, get the datetime stamp for the next occurrence of 22:30:00 which is 2019-11-27 22:30:00 as per the given example.
answer found but can we optimize the code more ?
$a = explode(':', date('H:i:s', strtotime($validateData['time'])));
$str = '+'.$a[0].' hours '.$a[1].' minutes '.$a[2].' seconds';
$trigger_at = date(date('Y-m-d H:i:s', strtotime( $str, strtotime($validateData['date']))));
return $trigger_at;
This is simpler and a lot more readable
$time = "21:30:00"; // Time from input
$today = date("Y-m-d $time");
$tomorrow = date("Y-m-d H:i:s", strtotime($today)+86400);
$date = strtotime($today) < strtotime("now") ? $tomorrow : $today;
Explanation: We take timestamp at specified hour for today and tomorrow, if today timestamp has been passed, we use tomorrow timestamp. Simple. :)
All you are doing is appending (concatenating) a string onto another string.
$time = "22:30:00"; // This is the time you have
$date = date("Y-m-d"); // Right now in yyyy-mm-dd format.
$newdatetime = $date.' '.$time;
That will give you the current date with the supplied time appended to it. You can convert that back into a timestamp using:
$timestamp = strtotime($newdatetime);
The answer below is based on the original question in which the time was assumed to be an offset from now. It is left here simply to avoid deleting a lot of code.
The function strtotime is easy to use for that. However, it doesn't accept HH:MM:SS format. So, you have to alter the string. I would do it like:
$time = "22:30:00"; // This is the time you have
$a = explode(':', $time);
$str = '+'.$a[0].' hours '.$a[1].' minutes '.$a[2].' seconds'; // This breaks it into separate numbers with labels.
$date = date("Y-m-d h:i:s", strtotime($str)); // The adjusted date
You can change the format of the output as you like by changing the first string used in the date function.

PHP time elapsed with MySQL DateTime

I am writing a login system in PHP and would like a to implement a function that checks if the 15 minutes has elapsed since the last failed login attempt.
In the MySQL database in my users table there is a column called last_login_datetime which is defined as SQL's DATETIME format, ex. 2018-02-24 21:12:57.
This is the code I am using to check if 15 minutes has elapsed since the last login fail
$dateTime = date("Y-m-d H:i:s");
if($row['user_locked']==$statusY AND $dateTime - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}
This does not seem to work for me at the moment and was hoping if someone could point out where I am going wrong?
Thanks in advance.
It doesn't work because you are substracting unix time from a string date. What you need to do is to get $dateTime as unix time.
$dateTime = strtotime("now");
Just use strtotime($dateTime) instead of $dateTime in the if condition. strtotime returns a UNIX tiemstamp i.e: the number of seconds since January 1 1970 00:00:00 GMT
$dateTime = date("Y-m-d H:i:s");
if($row['user_locked']==$statusY AND strtotime($dateTime) - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}
Your code won't work because your date will be read as a string. Try this instead:
$dateTime = strtotime("now");
if($row['user_locked']==$statusY AND $dateTime - strtotime($row['last_loginfail_datetime'] > 15 * 60))
{
.....
}

Displaying a date in php 24 hours later, issues

I'm basically trying to add 24 hours to a date with php and display it but it keeps adding only 23 hours in stead of 24 hours.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo gmdate("Y-m-d H:i:s", $set_time);
?>
So the result that I'm getting out of this is:
2015-03-19 19:03:23
but it's this what should be coming out of this:
2015-03-19 20:03:23
I'm new at working with these time functions and I can't figure out why it keeps getting adding 23 hours. Obviously I can multiply it by 25 and get 24 hours but that doesn't make sense to me.
So my question is: what's the proper way to add 24 hours to a date?
I would do it like so:
date("Y-m-d H:i:s", strtotime("+1 day"));
strtotime() uses default time zone, gmdate() uses Greenwich Mean Time (GMT). Try using date() instead.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo date("Y-m-d H:i:s", $set_time);
?>

Take current datetime and make 2 datetime variables? (6AM - 5:59AM)

I'm trying to make an events page and I want to create 2 datetime variables. They would take the current time, and create one variable at 06:00 am, and one at 05:59 am.
The issue I'm having though is with the calculations.
If a person is visiting the page on March 17, 11PM - then var1 would be March 17 06:00AM, and var 2 March 18 05:59AM.
However if a person is viewing the page on March 18 01:00 AM, then var 1 would still be March 17 06:00AM, the same goes for var2.
How would I take the below $date variable, and do the calculations for the other 2 variables?
date_default_timezone_set('America/New_York');
$date = date('Y-m-d H:i:s', time());
You can simply query the current hour to see if it's less than 6; if it is, then the start of the current logical day (based on your rules) was yesterday, 6am; otherwise it was today, 6am. Given this, strtotime can trivially get you the "start" time and adding a day to that gives you the "end" time.
date_default_timezone_set('America/New_York');
$currentHour = date('H');
if ($currentHour < 6) {
// logical day started yesterday
$start = strtotime('yesterday 06:00');
$end = strtotime('today 05:59:59');
}
else {
// logical day started today
$start = strtotime('today 06:00');
$end = strtotime('tomorrow 05:59:59');
}
echo "The current logical day started on ".date('Y-m-d H:i:s', $start);
echo " and it ends on ".date('Y-m-d H:i:s', $end);
The method for chopping the date up could be improved, but the principle works...
<?php
date_default_timezone_set('America/New_York');
$date = date('Y-m-d H:i:s', time());
// get adjusted date which subtracts 6 hours
$date_adjusted = date('Y-m-d H:i:s', time() - 60 * 60 * 6);
// chop off the time (so we are always left with the correct date now)
$date_adjusted_date = preg_split("/ /",$date_adjusted);
// Add the time element (in this case 6 AM)
$correct_date = date('Y-m-d H:i:s', strtotime($date_adjusted_date[0]."T06:00:00"));
// check the result
echo $correct_date;
?>

Categories