Mysql database timestamp query using php
I inserted into database a timestamp value set to 3 hours a head as follows
//For 3 hours ahead of time
$reciever_time = date("Y-m-d H:i:s", strtotime('+3 hours'));
now once its upto 3 hours, i need to echo "3 hours is due" but the code below keeps echoing "its not yet 3 hours" even though
3 hours is due. any help
<?php
$link = mysqli_connect("localhost", "sectona", "secBBN", "sectonadb");
$q = "SELECT id,reciever_time FROM indextime WHERE id='10'";
$qu = mysqli_query($link, $q);
while($row=mysqli_fetch_array($qu, MYSQL_ASSOC)){
$id = $row['id'];
$timest = $row['reciever_time'];
}
//60secx10min is 600milisec
//60secx30min is 1800
// 30 min = 1800
// 1 hour = 1800 x2
//12 hour = 3600 x12
//1 day = 3600 x24
$time = strtotime($timest);
$curtime = time();
// execute data that is due in 3 hours
if(($curtime-$time) >= 10800) {
print "3 hours is due then update";
}
else{
print "its not yet 3 hours";
}
?>
Try this:
if(($time - $curtime) >= -10800) {
print "3 hours is due then update";
} else {
print "its not yet 3 hours";
}
Only convert the 10800 to negative.
You can try:
$link = mysqli_connect("host","user","password","DBname");
$q = "SELECT id,reciever_time FROM indextime WHERE id='10' LIMIT 1";
$qu = $link->query($q);
$row = mysqli_fetch_object($qu);
$id = $row->id;
$timest = $row->reciever_time;
$time = strtotime($timest);
$curtime = time();
$message = ($curtime - $time) >= 10800 ? "3 hours is due then update" : "its not yet 3 hours";
print $message;
in PHP you want to use the the date_diff function to get the difference in time
try:
if(date_diff($time,$curtime) >= 10800){
Related
I have saved a time. I would like to check whether the saved time is 30 minutes greater than the current time. You can see in the code what I have done so far but it doesn't work.
I would need some help fixing that code.
$current = "08:05";
$to_check = date('h:i');
if($to_check > $current + strtotime('30 minute')){
echo "greater";
}
else {
echo "not greater";
}
First, your $current isn't the current time, $to_check is, so your variable names are misleading.
That being said, store your "08:05" as a Unix timestamp then see if the difference between the two is greater than 30 * 60.
$seconds = 1800; // 30 mins
$time1 = strtotime($db_time) + $seconds;
if(time() >= $time1)
echo "Time's up!"
else
echo "time has not expired";
This should work for you:
<?php
$current = date('H:i');
$to_check = date('H:i', strtotime('+30 minutes'));
$to_check = date('H:i', strtotime($record['date'])); //If value is from DB
if($to_check > $current){
echo "greater";
}
else {
echo "not greater";
}
Save time using time function this would give you time in seconds from 1970 so for example current time is 1501137539 and after 30 minutes it would be (1501137539 + 30*60) so you would just need to check if difference b/w current time and stored time is greater that 30*60 then half an hour is over.
Try this snippet:
function x_seconds($to_check = 'YYYY-mm-dd H:i:s') {
$to_check = strtotime($to_check);
$current = time() + 1800; //We add 1800 seconds because it equals to 30 minutes
return ($current>=$to_check) ? true : false;
}
Or go pro, and have a custom "seconds since", just because you can?
function x_seconds($to_check = 'YYYY-mm-dd H:i:s', $seconds = 1800) {
$to_check = strtotime($to_check);
$current = time() + $seconds; //We add x seconds
return ($current>=$to_check) ? true : false;
}
Example usage:
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
print "Is within 30 minutes.";
else:
print "IS NOT within 30 minutes";
endif;
Try this to solve the issue
$current = strtotime('12:00:00');
$to_check = strtotime(date('H:i:s'));
$newtime = round(( $to_check - $current ) /60 );
if($newtime > 30){
echo "greater";
}else {
echo "not greater";
}
My server time is GMT, so I convert it to Asia/Kuala Lumpur timing.I need to calculate time difference between current time and the time request was made. The time request was made is stored in database and retrieved in $reset_req variable.
$reset_req="2015-06-30 11:30:23";
$timezone_offset = +8; // us central time (gmt-6) for me
if(isset($reset_req)){
$request_date2 = strtotime($reset_req)+$timezone_offset*60*60;
}
echo "current time= ".strtotime(time());
echo"<br/>";
echo "time req made=".strtotime($request_date2);
echo"<br/>";
$timediff = strtotime(time()) - strtotime($request_date2); // in seconds
if($timediff < 2 hours)//how to calculate 2 hours here
{
//do something
}
Everyone already said how to calculate 2 hours (2* 3600sec). But what they didn't tell you is that you can't do this: strtotime(time()); time(); already gives you a Unix timestamp so you can't convert it twice.
Your Code should look more like this
<?php
$reset_req = "2015-06-30 11:30:23";
$timezone_offset = +8; // us central time (gmt-6) for me
if(isset($reset_req)){
$request_date2 = strtotime($reset_req)+$timezone_offset*60*60;
$current = time();
} else {
echo '$reset_req was not set';
exit;
}
echo "current time= " . $current . "<br />";
echo "time req made=" . $request_date2 . "<br />";
$timediff = $current - $request_date2; // in seconds
if($timediff < (2*3600))//how to calculate 2 hours here
{
echo "less than 7200 sec have past since $request_date2. Past: $timediff seconds";
}else{
echo "more than 7200 sec have past since $request_date2. Past: $timediff seconds";
}
?>
$hour = 3600; //an hour has 3600 seconds
if($timediff < 2 * $hour )
{
//do something
}
$timediff is in seconds. So you have to convert that to hours or convert the 2 hours to seconds and then need to compare like as follows:
replace
if($timediff < 2 hours)//how to calculate 2 hours here
{
//do something
}
with
if($timediff < (2*3600))//how to calculate 2 hours here
{
//do something
}
Try this
<?php
$reset_req = "2015-06-30 11:30:23";
//set default timezone here
date_default_timezone_set("Asia/Kuala_Lumpur");
$request_date2 = '';
if(isset($reset_req))
{
$request_date2 = strtotime($reset_req);
}
echo "Current time = ".time();
echo"<br/>";
echo "Time req made =".$request_date2;
echo"<br/>";
$timediff = time() - $request_date2; // in seconds
echo "<br>Time Differnce : ".$timediff;
//how to calculate 2 hours here
if($timediff < 2*3600)
{
//do something
}
?>
The following Mysql SELECT-part of Timestampdiff
$result_all = mysql_query("SELECT *, TIMESTAMPDIFF(MINUTE,aktualisiert,NOW()) AS quote_diff FROM sms WHERE gesendet ='1' AND DATE_FORMAT( aktualisiert, '%Y' ) = '2015' AND trash ='0' ORDER BY aktualisiert DESC");
seems not to work with the if else PHP Code
<?php
$qd = $row['quote_diff'];
$qdint = intval($qd);
$zahl = intval(100);
echo $qdint;
if ( $qdint > $zahl) {echo " minutes"; }
else { echo " minute"; }
var_dump($qdint); //int(91)
var_dump($zahl); //int(100)
?>
Also var_dump says, there are both integer.
But why is there no reaction of is bigger than > (or is smaller than < or is equal to ==)?
Found a working solution, without Timestampdiff:
$result_all = mysql_query("SELECT * FROM sms WHERE gesendet ='1' AND DATE_FORMAT( aktualisiert, '%Y' ) = '2015' AND trash ='0' ORDER BY aktualisiert DESC");
(because a known problem dev.mysql.com...12.7...user comment from April 9 2004
<?php $now = time();
$lastLogin = strtotime($row['aktualisiert']);
$diff = $now - $lastLogin;
$now = date('YmdHis',$now);
$diffecho = idate('i',$diff);
if($diff > 59 && $diff < 119) { // 3600 seconds is 1 hour
echo $diffecho." minute ago"; }
else { echo $diffecho." minutes ago"; }
?>
Use the PHP function idate, if you don't want a leading zero.
There is also another cool way on developphp.com:
Convert MySQL Timestamp to Ago Time Format OOP Tutorial
Maybe I use this later.
Thanks
Difference is a MySQL time. I am trying to check if the MySQL time returned is greater than 5 minutes. I have tried the code below but it doesn't seem to be working.
if (strtotime($myMySQLTimeValue) > strtotime("+5 minutes",)) {
// My code if MySQL time is greater than 5 minutes
}
Any ideas?
If you already have the time in the format 00:06:43 then you could use the following code
$diff_time = "00:06:43";
$diff_arr = split(":", $diff_time);
// Check mins and hours
if ( intval($diff_arr[1]) >= 5 || intval($diff_arr[0]) > 0) {
echo "Time more then 5 min";
} else {
echo "Time less then 5 min";
}
Assuming you retrieved mysql time by "SELECT NOW()", you can use below comparison:
$_5minuteslater = time() + 5 * 60;
if ($mysqlTime > $_5minuteslater) {
}
How can I countdown for 7 hours from a variable time (I will get time from my table which is inserted with timestamp), after 7 hours from variable time I will update a table.
I need something like that
$time = 2013-05-18 02:00:00 // comes from database
$target = $time + 7hours // time from database +7hours will be 2013-05-18 09:00:00
$until = $target - $time
I need something like below code
if ($until > 0 ) {
echo "you need to wait for $until hours"
} else {
echo "time is ok"; // i will update a table
}
Convert time into string using strtotime($time)+25200 where 7 hour =60*60*7=25200 sec and then check and also add this file to your cron job.
So, considering that $database_time is the stored time, in your db, and $time_now is your computers time, this message below, just echoed out:
You Must wait 4 hours right, now
It could be done much better, but still calculates the hours from now, to db and tells you how much more, you must wait :)
<?php
$date = date_create();
$database_time = '2013-05-18 22:00:00';
$time_now = date_format($date, 'Y-m-d H:i:s');
$check = $database_time[11];
$check .= $database_time[12];
$check2 = $time_now[11];
$check2 .= $time_now[12];
$time_left = $check - $check2;
So, here is how you can manage your ourputs
if($time_left > 0) {
echo "You Must wait $time_left hours right, now";
}else{
echo "Time is OK";
}