Strtotime not working as expected - php

My goal is to compare the current time with the last visited time and if was 5 minutes ago then allow to visit again, else, deny! Here what I have so far
$last_activate = strtotime($last_activ); //$last_activ is TIMESTAMP value retrieved from MySQL database
$current_time = strtotime('now');
if(($current_time - $last_activate) > strtotime('5 minutes')){
//allow access
}
else{
//deny access
}
At the moment, the code above always executes else statement even if $last_activate was 24 hours ago. Anyone knows what point I am missing?

strtotime('5 minutes') means now + 5 minutes. Proof:
echo date("Y-m-d H:i:s", strtotime('5 minutes'));
2013-03-21 19:41:27
Do this instead:
if(($current_time - $last_activate) > 5 * 60){

Replace 5 minutes with 5 minutes ago.

Related

PHP Touch() using time() parameter for the past -any limitation?

When changing a file's datetimestamp from the current system time to the distant past is there a limit with the time() parameter? With touch(), all the documentation I can see uses the time() parameter which has a future limitation of 100000 seconds:
<?php
$file_pointer = "gfg.txt";
// setting touch time to 5 hours in the past
$time = time() - 18000;
// using touch() function to change the modification
// time of a file to current system time
if (touch($file_pointer, $time))
{
echo ("$file_pointer modification time has been changed to 5 hours in the past.");
}
else
{
echo ("$file_pointer modification time cannot be changed.");
}
?>
What would be the best approach to change it to 1 year in the past or even 5 years in the past? Would the following be the most accepted approach and would it work?:
$time = time() - 31536000;// 1 year
$time = time() - 157680000;// 5 years
use this docs
$time = strtotime("5 years ago");
// or
$time = strtotime("-5 years")

PHP check if pass 5 minutes between two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I am trying to check if pass 5 minutes between two dates.
Here is my code:
$time = strtotime("+5 minutes").'<br>';
$var = '1518219956';
$e1 = date("d-m-Y H:i",$time);
$e2 = date("d-m-Y H:i",$var);
if($var > $time){
echo 'true<br>';
echo 'Time: '.$e1.'<br> Var:'.$e2;
} else{
echo 'false<br>';
echo 'Time: '.$e1.'<br> Var: '.$e2;
}
I am sorry but I lost myself with this timestamps..
Sorry, I don't mean to be rude but I was confused by the question. What I designed here was to see if time B is five minutes after Time A.
A few notes:
No need to bother with strtotime or date. Just keep everything in unix time. Compare the seconds by using 60 * 5. 60 for 60 seconds in a minute and 5 for the number of minutes.
<?php
//Time A
$timeA = '1518223062';
//Time B (Which I set at the current time)
$timeB = time();
//five minutes in seconds
$fiveMinutes = 60 * 5;
//check if current time is after 5 minutes the initial time
if ( ($timeA+$fiveMinutes) <= $timeB) {
echo "True";
}
else {
echo "False";
}
?>

PHP get time different in string

I have a time string, the first user's last login : '2014.12.01, 12:25'
And how to calculate the time different from now? For e.g. another user see the first user's last login
At 12:30 and it says 5 minute ago.
Or at 13:23 says 48 minute ago.
Or at 14:24 says 1 hour ago.
Or one day later, it says 1 day ago.
You can try the following:
$to_time = strtotime("2014-12-01 12:25:00");
$from_time = strtotime("2014-12-01 12:25:00");
$minutes = round(abs($to_time - $from_time) / 60,2);
$seconds = abs($to_time - $from_time) % 60;
echo "$minutes minute, $seconds seconds";
It had already been answered at the following link:
"Calculate time different in minute and second"
Hope this helps
$first_user_time = '2014-12-01 12:30:00';
$curnt_user_time = '2014-12-01 12:40:00';
$diff = date('h:i:s', strtotime($first_user_time)-strtotime($curnt_user_time))
echo $diff;
//it will give diff in hour : min : seconds

Time difference php/mysql need to format the output conditionally (4 small highlights required)

im trying to get difference of date/time from a field type datetime to "right now" using php and mysql as database
this code is working fine, returns the output beautifully ok as required
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%d days %h hours %i minutes');
that is ok so far, no issues as this function is for php 5.3 and i have it on server
my need is 4 small things actually
1) how to eliminate the need for days?
i want to have (25 hours 10 minutes) instead of (1 day 1 hours 10 minutes)
2) how i can make $elapsed be bold or colored if the value is more than 5 hours for example!? simple IF logic will not work as the output is not actually a predefined value...
3) if the days or hours are 0, then want to remove them!
- For example if showing (0 days 10 hours 40 mins) then no need to display the (0 days), should show (10 hours 40 mins) that is enough
- Another example: 0 days 0 hours 45 minutes then to show only "45 minutes" no need for days and hours!
4) if output less than 5 minutes in total (0days 0 hours 1-5mins), then wanna make it show like "a while ago" only no need for any days, hours or minutes... then after 6 minutes.. go like "6 mins"
shortly something like facebook!?
okay, what i searched tried is different combination of workarounds but never worked as you know this interval is for php 5.3 and still seem not widely used?
any hint for one or more parts of this long question is appreciated,
M, Derik
tried to answer all your questions. so read the comments because i didnt write numbers for the problems. sorry
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$minutes = round(abs($datetime1 - $datetime2) / 60,2); //to calculate total time in MINUTES
if($minutes < 5) // for awhile ago problem.
{
return "awhile ago";
}
elseif($minutes > 6 && < 60)
{
return $minutes." minutes ago"; //for 6 minutes and after.
}
elseif($minutes>60) // for the hours..
{
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
$string = $hours. " hours and" . $minutes . " minutes ago.";
if($hours>5) //for bolding characters after
{
return "<b>".$hours." hours and".$minutes." minutes ago.</b>"; //for bolding character
}
else
return $hours." hours and".$minutes." minutes ago.";
}
hope this helps.

PHP Checking if timestamp is less than 30 minutes old

I'm getting a list of items from my database, each has a CURRENT_TIMESTAMP which i have changed into 'x minutes ago' with the help of timeago. So that's working fine. But the problem is i also want a "NEW" banner on items which are less than 30 minutes old. How can i take the generated timestamp (for example: 2012-07-18 21:11:12) and say if it's less than 30 minutes from the current time, then echo the "NEW" banner on that item.
Use strtotime("-30 minutes") and then see if your row's timestamp is greater than that.
Example:
<?php
if(strtotime($mysql_timestamp) > strtotime("-30 minutes")) {
$this_is_new = true;
}
?>
I'm using strtotime() twice here to get unix timestamps for your mysql date, and then again to get what the timestamp was 30 minutes ago. If the timestamp from 30 mins ago is greater than the timestamp of the mysql record, then it must have been created more than 30 minutes go.
Try something like this, using PHP's DateTime object:
$now = new DateTime();
$then = DateTime($timestamp); // "2012-07-18 21:11:12" for example
$diff = $now->diff($then);
$minutes = ($diff->format('%a') * 1440) + // total days converted to minutes
($diff->format('%h') * 60) + // hours converted to minutes
$diff->format('%i'); // minutes
if ($minutes <= 30) {
echo "NEW";
}
Edit: Mike is right, I forgot that for whatever reason, only %a actually returns the total of its type (in this case days). All the others are for displaying time formatting. I've extended the above to actually work.
You can do like this also -
$currentTime=time();
to check the last updated time is 30 minute old or not
last_updated_at < $currentTime - (60*30)

Categories