In my table there is a created datetime field in which data inserting like 2016-05-25 11:50:35.
Suppose my insertion time is 11:50 and current time is 12:10,so the differnce is 20 mins.
now i need to check is get this difference in one variable and check if differnce > 30 then the edit link will be invisible else visible.
below is my code..
i get extracted mins from my created datetime field from database.
$var1 = $post['ShipperRating']['created'];
$time1 = date('i', strtotime($var1));
$finaltime = $time1;
echo $finaltime;
$createdDateTime = new DateTime($post['ShipperRating']['created']);
$createdDateTime->modify('+30 minutes');
if ($createdDateTime >= new DateTime()) {
echo 'Edit';
}
Using strtotime (http://php.net/manual/en/function.strtotime.php) will work...
$var1 = $post['ShipperRating']['created'];
$time1 = strtotime($var1);
$finaltime = strtotime('+ 30 minutes',$time1);
echo date('Y-m-d H:i:s',$finaltime);
If you want to do it by PHP then you have to refresh page after 30 minutes to and calculate the time from server which already show in other answers. You can use header function to set time for refreshing page
header("Refresh: 300;url='http://example.com/example'");
Use following Function
(1800000 miliseconds =30mintues)
window.setTimeout('removelink()',1800000); //
function removelink()
{
//write Code to Remove your Link
}
Related
I am working on email link expire after some X minutes where X denotes some random date_time. so my motive is to expire the the link after some time what ever I set the date_time in side the $expire_date.
So I just created dummy code myself just in order to sure my code works or not.
$currentDateTime = new \DateTime();
$currentDateTime-> setTimezone(new \DateTimeZone('Asia/kolkata'));
$now = $currentDateTime-> format(' h:iA j-M-Y ');
$expire_date = "02:59PM 26-Mar-2019";
if($now > $expire_date)
{
echo " link is expired";
}
else{
echo " link still alive ";
}
I guess I am missing something in the above code, somehow the above code isn't working if anyone would point out the right direction or some better implementation it would be great.
You are comparing the times as strings. This does not work, as your first formatted string has a leading space.
Instead, try either removing the whitespace, or better, compare the times as DateTime objects:
$timezone = new \DateTimeZone('Asia/kolkata');
// Create the current DateTime object
$currentDateTime = new \DateTime();
$currentDateTime-> setTimezone($timezone);
// Create the given DateTime object
$expire_date = "02:59PM 26-Mar-2019";
$expireDateTime = \DateTime::createFromFormat($expire_date, 'h:iA j-M-Y');
// Compare the objects
if($currentDateTime > $expireDateTime)
{
echo " link is expired";
}
else{
echo " link still alive ";
}
If you want to compare dates in PHP, your best bet is to use UNIX time stamps. A UNIX time stamp is the number of seconds since the UNIX epoch (00:00:00 Thursday, 1 January 1970).
time() will return the current UNIX time stamp.
strtotime() will convert a date string into a UNIX time stamp.
So replacing these two lines:
$now = $currentDateTime-> format(' h:iA j-M-Y ');
$expire_date = "02:59PM 26-Mar-2019";
With these:
$now = time();
$expire_date = strtotime("02:59PM 26-Mar-2019");
Should solve your problem.
You are comparing date strings which will not work. You have to parse the string to a datetime object or timestamp before you can compare these values.
For example, using timestamps:
$expire_date = "02:59PM 26-Mar-2019";
if (time() > strtotime($expire_date)) {
echo "link is expired";
} else {
echo "link still alive ";
}
All you have to do is use strtotime function and add inside date function and here you can specify day, hour, minutes, seconds as a perimeter. This way you can set time manually by adding +5 minutes or so on..
date_default_timezone_set("Asia/Kolkata"); // set time_zone according to your location
$created = "2020-08-14 17:52"; // time when link is created
$expire_date = date('Y-m-d H:i',strtotime('+1 minutes',strtotime($created)));
//+1 day = adds 1 day
//+1 hour = adds 1 hour
//+10 minutes = adds 10 minutes
//+10 seconds = adds 10 seconds
//To sub-tract time its the same except a - is used instead of a +
$now = date("Y-m-d H:i:s"); //current time
if ($now>$expire_date) { //if current time is greater then created time
echo " Your link is expired";
}
else //still has a time
{
echo " link is still alive";
}
How do I remove time from a date in PHP, for example:
20170803173418 I want to take 4 minutes and 13 seconds away and get the new datestamp that would be 20170803173005
What code do I use to get this?
EDIT
I currently have:
$dbTime = $row['aptDeadline']; // This is the appointment end time stored in DB
$dbTime = new DateTime($dbTime);
$currentTime = date("YmdHis"); // This is the current time
$currentTime = new DateTime($currentTime);
$counterTime = $row['aptDeadline']; //This is the time a countdown clock works from inDB
$counterTime = new DateTime($counterTime);
$difference = $currentTime->diff(new DateTime($dbTime)); // Calculate the time between now and the apt time in the db
I now need some code that if the $difference is positive, can take this figure away from the $counterTime stamp
You can use the modify method of the DateTime class in PHP:
<?php
$time = new \DateTime('20170803173418');
$time->modify('-4 minutes')->modify('-13 seconds');
echo $time->format('YmdHis');
This will print the result you want.
I want to check if 30 min passed after created time in database. created is a time column having time stamp in this format 1374766406
I have tried to check with date('m-d-y H:i, $created) but than of course it is giving human readable output so don't know how to perform check if current time is not reached to 30min of created time.
Something like if(created > 30){}
Try this:
$created = // get value of column by mysql and save it here.
if ($created >= strtotime("-30 minutes")) {
// its over 30 minutes old
}
The better approach is to use DateTime for (PHP 5 >= 5.3.0)
$datenow = new DateTime();
$datenow->getTimestamp();
$datedb = new DateTime();
$datedb->setTimestamp(1374766406);
$interval = $datenow->diff($datedb);
$minutes = $interval->format('%i');
$minutes will give you the difference in minutes, check here for more
http://in3.php.net/manual/en/datetime.diff.php
Here is the working code
http://phpfiddle.org/main/code/jxv-eyg
You need to use strtotime(); to convert the date in human form back to a timestamp, then you can compare.
EDIT: Maybe I misread.
So something like;
if(($epoch_from_db - time()) >= 1800){
//do something
}
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 )
How can I identify the time passed after an user updated his account in my MySQL database? I have a timestamp in my MySQL table (to store user update time) so now how can I identify the time passed from last user update, using PHP?
As example:
User last update time: 2012-04-08 00:20:00;
Now: 2012-04-08 00:40:00;
Time passed since last update: 20 (minutes) ← I need this using PHP
If you have the data on
$last_update_time = '2012-04-08 00:20:00';
$now = time(); //this gets you the seconds since epoch
you can do
$last_update_since_epoch = strtotime($last_update_time); //this converts the string to the seconds since epoch
aand... now, since you have seconds on both variables
$seconds_passed = $now - $last_update_since_epoch;
now, you can do $seconds_passed / 60 to get the minutes passed since the last update.
Hope this helps ;)
In pseudo-code:
$get_user_time = mysql_query("SELECT timestamp FROM table");
$row = mysql_fetch_array($get_user_time);
$user_time = $row['timestamp']; //This is the last update time for the user
$now_unformatted = date('m/d/Y h:i:s a', time());
$now = date("m/d/y g:i A", $now_unformatted); // This is the current time
$difference = $now->diff($user_time); // This is the difference
echo $difference;
diff() is supported in >= PHP 5.3. Otherwise you could do:
$difference = time() - $user_time;
$secs=time()-strtotime($timestamp);
would give u number of seconds before it was updated and then you can convert this seconds into your needed time format like
echo $secs/60." minutes ago";
why not do it with mysql:
select TIMEDIFF(NOW(),db_timestamp) as time_past