If time now is more than 3 hours since last update - php

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

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 date() is greater than a specific time

In my code I pretty much send a token to the database with the
date('Y-m-d H:i:s');
function. But I am checking to see if the timestamp I sent if greater than 60 seconds if so i echo yes and else no. I keep getting no and I know its more than a minute because i time it. I've seen post about this but they're using specific dates and I am just going on when a user submits a form and checking if the token is 60 seconds old. Here is my code
php
<?php
require_once('db_login.php');
$stmtToken = $handler->prepare("SELECT * FROM email_token");
$stmtToken->execute();
$rowToken = $stmtToken->fetch();
$date = $rowToken['time_stamp'];
if($date > time() + 60) {
echo 'yes';
} else {
echo 'no';
}
?>
You can also play with dates in different manners. All four lines here are equivalent:
$now = (new \DateTime(date('Y-m-d H:i:s')))->getTimestamp();
$now = (new \DateTime('now'))->getTimestamp();
$now = (new \DateTime())->getTimestamp();
$now = time();
And then you can compare in this manner:
$tokenExpirationTimestamp = (new \DateTime($date))
->modify('+60 seconds')
->getTimestamp();
$isTokenExpired = $tokenExpirationTimestamp < time();
if ($isTokenExpired) {
// ...
}
When you compare times and dates you can either use datetime or strtotime.
Using strings will not work as expected in all cases.
In comments you mentioned how you want to compare, and you need to add the 60 seconds to the "date", not the time().
if(strtotime($date) + 60 < time()) {

Comparing time in 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>';
}

How to calculate the time difference between unix time stamps?

I am creating time stamps in PHP using time();
I have the $current_time and $purchase_time. How do I make sure that purchase_time is less than 24 hours of current time?
If they are UNIX timestamps, then you can calculate this by yourself really easy, as they are seconds.
$seconds = $current_time - $purchase_time
$hours = floor($seconds/3600);
if ($hours < 24){
//success
}
Since UNIX timestamps are just numbers of seconds, just use the difference:
$purchasedToday = $current_time - $purchase_time < 24 * 60 * 60;
if ($purchasedToday) {
echo 'You just bought the item';
} else {
echo 'You bought the item some time ago';
}
You can construct a DateTime object and then use it's diff() method to calculate the difference between $current_time and $purchase_time:
$currentTime = new DateTime();
$purchaseTime = new DateTime('2011-10-14 12:34:56');
// Calculate difference:
$difference = $currentTime->diff($purchaseTime);
if ($difference->days >= 1) {
echo 'More than 24 hours ago.';
}
This is more reliable than calculating the difference yourself, as this method takes care of timezones and daylight saving time.
Something like this:
$difference=time() - $last_login;
I had use something like this:
<?php
if(date("U", strtotime("-24 hours", $current_time) > date("U", $purchase_time)) {
echo "More then 24 hours you purchased this radio";
}
?>
This works even if the time stamp not is a UNIX-timestamp.

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