I'm having some trouble calculating if time is past 15 minutes, on a date and time in my database. How can I check this?
I have this in my database (example):
2014-01-30 15:29:31
And then I get the date from PHP:
$date = date("Y-m-d H:i:s");
But how can I check if the time has passed 15 minutes, or if it's a different day?
I have this code so far:
$date = date("Y-m-d H:i:s");
$sql = "SELECT date FROM reset WHERE session_id = '".$sessid."' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$dbdate = $value->date;
$checkdate = strtotime($dbdate);
if ($checkdate - time() > 15 * 60) {
error_log("15 mins passed");
}
You can convert the date to a timestamp with strtotime (which supports the MySQL date format) and then compare it to the current timestamp from time.
$dbtimestamp = strtotime($datefromdb);
if (time() - $dbtimestamp > 15 * 60) {
// 15 mins has passed
}
To compare the dates, you can use date to get the year/month/day from the timestamp and then compare them against the current date.
if (date("Y-m-d", $dbtimestamp) != date("Y-m-d")) {
// different date
}
Using a DateTime object:
$dateTimeObject = new \DateTime($dateString);
//and subtract using an interval
$dateTimeObject->sub(new \DateInterval("PT15M"));
$newDateString = $dateTimeObject->format("Y-m-d H:i:s");
you cans use DATE_FORMAT functions.
$actual_minute = date("i");
$data = mysql_query("SELECT DATE_FORMAT(date,'%i') minutes FROM reset WHERE session_id = '".$sessid."'");
$database_minute = $data{'minutes'};
Related
I need to extract only the day number of a user's registration date.
And extract only the day number of the current date.
Simply in an if loop, say if the day number the user registered is equal to the day number of the current date, do this, or do that.
Code:
$manager = "Manager";
$managerPRO = "ManagerPRO";
$q = $connessione->prepare("
SELECT * FROM collaboratori
WHERE cat_professionisti = ?
OR cat_professionisti = ?
");
$q->bind_param('ss', $manager,$managerPRO);
$q->execute();
$r = $q->get_result();
while($rr = mysqli_fetch_assoc($r)){
/*REGISTRATION DATE*/
$registrazione = $rr['data_registrazione'];
$timestamp = strtotime($registrazione);
echo date("d", $timestamp) .'=' ;
/*CURRENT DATE*/
$data_corrente = date('Y-m-d');
$timestamp_uno = strtotime($data_corrente);
echo date("d", $timestamp_uno);
/*CONTROL*/
if ($timestamp == $timestamp_uno){
echo "yes".'<br>';
}else{
echo "no".'<br>';
}
}
Result:
18=18no
17=18no
16=18no
16=18no
Why in the first case if 18 = 18 gives me false?
However, if I change the date of the user's registration and therefore the first 18, from 2020/11/18 to 2020/12/18, then the current month gives me yes!
I need that regardless of the month, just by checking the day if it is the same, tell me yes, where am I wrong?
You are comparing timestamps, which are measured in seconds. What you are doing is effectively comparing two different points in time, not the days of the month.
You really should be using DateTime. If you want to compare only the day part then you can do something like this.
$dt1 = new DateTime($registrazione);
$dt2 = new DateTime(); // defaults to now
if($dt1->format('d') === $dt2->format('d')) {
echo "Yes, it's the same day of the month";
} else {
echo 'no!';
}
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()) {
Actually i need to list out the data which is not exceeds 24 hours from the mail Date and Time.
I have stored the Mail Date and Mail Time in 2 different columns in database.
Please give your valid inputs from the below query I wrote,
$CTime = strtotime(date('Y-m-d H:i:s'));
//getting the 24 hours back time
$Btime = $CTime - 86400;
$listingInvoice = mysqli_query($conn, "SELECT *, (CONCAT(MailDate, MailTime)) AS MailDT FROM approved WHERE JobStatus='MailedToClient' && MailDT > '$Btime'");
Please check the date and time taken from different columns of database and find the value i.e., not exceed 24 hrs from the current time.
date_default_timezone_set('Asia/Kolkata');
//getting the current date and time...
$CTime = date('Y-m-d H:i:s');
//convert to string the current date and time...
$CSTime = strtotime(date('Y-m-d H:i:s'));
//Minus the 86400(value of 24 Hrs in seconds) in the converted date and time string
$BStime = $CSTime - 86400;
//getting the value of date and time before 24 Hrs...
$BTime = date('Y-m-d H:i:s', $BStime);
//Now the query is...
$reject = mysqli_query($conn, "SELECT * FROM approved WHERE JobStatus='MailToClient' && MailDateTime BETWEEN '$BTime' AND '$CTime'");
$row = mysqli_num_rows($reject);
if ($row == '0')
{
echo "No Jobs Mails SENT for the Last 24 Hrs";
}
else
{
....
}
Am using formula like this
<?php
//Coming From Mobile Phone
$Currenttime = 12:40:00;
//Server time Is Current Time of various timezone like(America/Chicago,America/Denver..)
$ServerTime = 12:55:00;
//Scheduletime is coming from database
$ScheduleTime = 14:45:00;
$ExactTime = ($Currenttime - $ServerTime) + $ScheduleTime
?>
I need Exact Time is like this 15:00:00
You should use the DateTime class for this:
$Currenttime = DateTime::createFromFormat('H:i:s', "12:40:00");
$ServerTime = DateTime::createFromFormat('H:i:s', "12:55:00");
$ScheduleTime = DateTime::createFromFormat('H:i:s', "14:45:00");
$ExactTime = $ScheduleTime->add($Currenttime->diff($ServerTime));
i tried most of what is available on stack overflow but none seem to work.
any way i am trying to compare two (date and time formats). and calculate whether their difference is within 5 seconds of each other
the first date is the current date:
$today = date("Y-m-d H:i:s");
the second date is taken from mysql database:
$result = mysql_query("SELECT * FROM BUS_DATA where BusRegID = 'bus'") or die(mysql_error());
$row = mysql_fetch_array($result);
$update_date = $row["update_date"];
the answer should be segmented into years , month , days , hours , minutes ,and seconds portions.
I am running PHP Version 5.3.3
Edit: most answers give result in time frame only , I want to check whether the date matches , then compare if the time of the day is within 5 seconds , than you guys in advance :)
Try this
function getTimes($t1, $t2)
{
$timeFirst = strtotime($t1);
$timeSecond = strtotime($t2);
$differenceInSeconds = $timeSecond - $timeFirst;
$h=0;
$m = floor($differenceInSeconds / 60);
$s = $differenceInSeconds % 60;
if ($m>=60)
{
$h = floor($m / 60);
$m = $m % 60;
}
$tim = $h.':'.$m.':'.$s;
return $tim;
}
it will return difference time in hours:min:sec
use strtotime, it's understands almost any date formats:
$timeDiff = abs(strtotime($today) - strtotime($update_date));
this gives you the difference between dates in seconds.
If you want to know whether it's within 5 seconds then use Timestamps, makes it into a simple int calculation.
$now = time();
$test = strtotime($update_date);
if($now - $test > 5) {
//NOT WITHIN 5 SECONDS
}