i got one problem where i nead your help, if you have any solution please help me.
The problem is as follows:
I m using $currenttime and $settime variable to set currenttime and targettime
$currenttime = date('Y-m-d H:i:s');
$settime = "2012-o5-03 02:10:00";
$diff1 = abs(strtotime($currenttime) - strtotime($settime));
On the basis of $diff1 we are starting the countdown and countdown are working properly.
Now i want to show a message when $settime and $currenttime are equal for these i have use this code
if(strtotime($currenttime) == strtotime($settime))
{
echo "Your time begin just now";
}
but after countdown reached to zero it should show the message but it not showing, if any one have any solution plz help me.
i have seen your code there was a little mistake with declaration such o instead of 0
& the code i wrote which is working as follow....
echo $currenttime = date('Y-m-d H:i:s');
echo $settime = "2012-05-03 12:20:50";
$diff1 = abs(strtotime($currenttime) - strtotime($settime));
if($currenttime != $settime)
{
echo "Your time not yet set";
}
else
{
echo "Your time begin just now";
}
I would tend to compare for a time later than the expected trigger time in case the script is not running at the exact second that current and trigger time are equal.
if(strtotime($currenttime) >= strtotime($settime))
{
echo "Your time begin just now";
}
Don't use equal, if your script-call is a bit late, you'll never get there. use either greater then > to do it for all times that are PAST your goal, or check if the difference between the times is something small (like abs(time1 - time2) < 2), so you know your current time is closer then 2 seconds from your target time.
Related
In the application that I'm working on, the user must choose a date/time which is at least 5 minutes into the future. For this, I'm trying to implement a check. Below is the code which checks the time difference between the current time and chosen time.
$cur_date = new DateTime();
$cur_date = $cur_date->modify("+1 hours"); //fix the time since its an hour behind
$cur_date = $cur_date->format('m/d/Y g:i A');
$to_time = strtotime($chosen_date);
$from_time = strtotime($cur_date);
echo round(abs($from_time - $to_time) / 60,2). " minute"; //check the time difference
This tells me the time difference from the chosen time and the current time in minutes. So let's say the current time is 09/22/2015 5:53 PM and the chosen time is 09/22/2015 5:41 PM - it will tell me the difference which is 12 minutes.
What I want to know is how I can tell if those 12 minutes are into the future or in the past. I want my application to only proceed if the chosen time is at least 5 minutes into the future.
You're doing too much work. Just use DateTime() to do the date math for you:
// Wrong way to do this. Work with timezones instead
$cur_date = (new DateTime()->modify("+1 hours"));
// Assuming acceptable format for $chosen_date
$to_time = new DateTime($chosen_date);
$diff = $cur_date->diff($to_time);
if ($diff->format('%R') === '-') {
// in the past
}
echo $diff->format('%i') . ' minutes';
Demo
$enteredDate = new DateTime($chosen_date)->getTimestamp();
$now = new DateTime()->getTimestamp();
if(($enteredDate-$now)/60 >=5)echo 'ok';
Basically, the code takes the two dates converted in seconds since 1/1/1970. We calculate the difference between the two dates and divide the result by 60 as we want minutes. If there is a difference of at least 5 minutes, we're ok. If the number is negative, then we are in the past.
If anyone is looking to do something similar, I found the Carbon library which is included by default with the framework I am using (Laravel 5), it was much easier to do this calculation.
$chosen_date = new Carbon($chosen_date, 'Europe/London');
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
echo "Chosen date must be after this date: ".$whitelist_date ."</br>";
echo "Chosen Date: ".$chosen_date ."</br>";
if ($chosen_date->gt($whitelist_date)) {
echo "proceed";
} else {
echo "dont proceed";
}
Context
I would like to use PHP to change out content in HTML every other day. I can currently achieve this, however, I would need the content to change at 7:00 AM PST. Right now, I'm essentially subtracting the current time from a set point in time using mktime and rounding down. A simple if statement takes care of the variable.
The Code
<?php
$first_date = mktime(7,0,0,1,1,2014);
$second_date = time();
$offset = $second_date-$first_date;
$this_day = floor($offset/60/60/24);
if ($this_day % 2 == 0) {
//do something
} else {
//do something else
}
?>
Like I stated before, the code is working however it is not changing at 7:00 AM PST. I tried adjusting the start time in $first_date however it didn't seem to help. I think I am missing something regarding actual timezones and how the time is being calculated. Any insight would be greatly appreciated.
I just take the start date and gave it a timezone, then checked the day number of the year. If it's even and 7am or later, do something. Otherwise, do something else. The only issue you will have is New Years. This may show the same content two days in a row.
<?php
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles'));
if ($date->format('z') % 2 == 0 && $date->format('h') >= 7) {
//do something
} else {
//do something else
}
?>
I've been doing a good amount of research with this, and used a few codes to get to know how to make this work, but nothing has worked the way I wanted it to, or hasn't worked at all.
The code is:
<?php
$time1 = $user['last_active'];
$time2 = "+5 minutes";
if (strtotime($time1) > strtotime($time2)) {
echo "Online!";
}else{
echo "Offline!";
}
?>
It is supposed to compare the two variables, and find out if the last active variable is greater or less than 5 minutes, and if it is greater, appear offline. I do not know what's wrong as the NOW() updates on each page and stops if the user is not logged in. Any suggestions or help? Thanks.
The $time1 variable is coming from a fetched array that gets the ['last_active'] information that updates on each page.
I fixed my code, but it still doesn't work right, however, I think I have managed to get further than I was..
<?php
$first = new DateTime();
$second = new DateTime($user['last_active']);
$diff = $first->diff( $second );
$diff->format( '%H:%I:%S' );
if($diff->format( '%H:%I:%S' ) > (strtotime("5 minutes"))){
echo "Offline";
}else{
echo "Online";
}
?>
What can I do at this point?
Nobody pointed out that you actually have a bug. The "current time" will never be greater than "the current time +5 minutes"
Your first code sample will work right if you instead use "-5 minutes" as the "online threshold."
Also, comparing a timestamp without date to the output of strtotime() as you do in the second code is not a proper comparison. It has two problems:
Each time a new day comes around, the same time value will be repeated.
The output of strtotime is an integer representing seconds-since-epoch; the output of format() is a textual representation of hours:minutes:seconds within the current date.
As for your question how to calculate time between 2 dates / time, please view the solution on the following posts, that should give you enough information! (duplicate ? )
Calculate elapsed time in php
And here
How to get time difference in minutes in PHP
EDIT AS YOU PLEASE
<?
$first = new DateTime(); // this would hold your [last active]
//$first->modify("-6 minutes");
$second = new DateTime("NOW");
$difference = $second->diff( $first ); // second diff first
if ($difference->format('%i') > 5) { // comparing minutes only in example ( %i )
echo "The user is AFK";
} else {
echo "user might still be active";
}
?>
I have created registration page and when user click submit button, an activation link is sent to his email and accordingly timestamp is stored in the database. If user click that activation link, I have to check whether that link is clicked before or after 24 hours .
my code :-
function confirmEmail($activation_code){
$this->load->database();
$this->load->helper('date');
echo "activation link will be checked and accordingly flag will be set.";
$activation_sent_timestamp=$this->db->query("SELECT activation_timestamp FROM tbl_user_registration WHERE email_verification_code='$activation_code'");
foreach($activation_sent_timestamp->result() as $res){
$activation_time_from_db=$res->activation_timestamp;
}
echo $activation_time_from_db."\n\r";
$now = time();
$human = unix_to_human($now);
echo $human;
$difference = ($human-$activation_time_from_db);
if($difference < 24) {
echo "correct"
}
else echo "Link expired";
}
I am using codeigniter. How can I do this, this code isnot showing any erros but I dont know is this the right way to calculate 24 hours, I am checking but didnt get anything.please check the code.
SOLVED........ :)
unix_to_human() just returns a human readable form of timestamp
The simplest method is, find the difference between both time stamps, convert to hrs and check if it is less than 24hrs
You may want to use date_diff
See here : DATE_DIFF()
You can use diff for calculation difference and also you can change time format
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Convert both timestamps to php date('Y-m-d') formats then try date_diff
You can use mysql timediff function in the query.
Looking at your situation you can do this as i have answered in this post
Finding free blocks of time in mysql and php?
$time = time();
$seconds = 86400 // seconds of 24 Hours
SELECT * FROM tbl_user_registration WHERE ( $time - UNIX_TIMESTAMP( activation_timestamp ) >= $seconds )
I am using the following code to attempt to compare the current date with a date entry in a mySql database. It's code that I have found online and adapted as all the examples I have found hard-code the date to compare the current date with.
The trouble is even dates in the future are being marked as expired and I can't understand why this would be.
I am afraid that I am still new to PHP, so I may be making a schoolboy error!
$exp_date = KT_formatDate($row_issue_whatson1['dateToShow']);
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) { echo "Not expired"; } else { echo "expired"; }
Any help would be most appreciated.
I should add that the date time format used in the database entries is dd/mm/yyyy
Instead of making a string then converting it to a timestamp, simply use mktime:
<?php
$today = mktime(
0, // hour
0, // minute
0 // seconds
);
?>
The rest of the values will be filled according to today's date. If this still gives problems, put in some echo's for the values of $exp_date and $expiration_date.
Edit
Since this solved the problem, the discrepancy you were seeing was because you were doing the opposite with date('d-m-Y'). You were asking for the current date and the time values are then filled in with the current time. The expiration date in the database is likely set at midnight. With both dates being equal, and it being say 11am now, you are comparing if (00:00:00 > 11:00:00) which fails.
$exp_date = 14/05/2011 // todays date, int
$server_date = server.date() // servers date, int
// check exp_date against server date
if ( $server > $exp_date)
{ echo "Sorry your 'service' has expired"; }
else
{ echo "Welcome 'members_name' to StackOverflow"; }
Try that. However you need the right date format, as server.date() is probably different in PHP.
If problem still persists I would check whether your dates are strings or integers or both. That could possibly be the issue.
Hope that helps.
DL.
Your function does not seem to be valid.
function KT_formatDate( $exp_date){
$exp_date = strtotime($exp_date);
$now = time();
if ($now > $exp_date)
return 'expired';
else
return ' Not expired';
}
$response = KT_formatDate($row_issue_whatson1['dateToShow']);