Could anyone solve my problem please.
I have mysql table with time_start 'hour:minute' and time_end 'hour:minute' how can i get cumulative time duration between time_start and time_end.
thanks,
$sel = $db->query("SELECT * FROM logbook WHERE date = '$date' ");
while($sel_row = mysqli_fetch_array($sel))
{
$duration = (strtotime($sel_row['time_end'])-strtotime($sel_row['time_start']))/(60*60);
$hour = floor($duration);
$minute = ($duration - $hour)*60;
echo $hour.":".$minute;
? HERE HOW CAN I CUMULATE THE $hour:$minute
}
Do the summation where you calculate the duration
$duration = 0;
while($sel_row = mysqli_fetch_array($sel))
{
$duration += (strtotime($sel_row['time_end'])-strtotime($sel_row['time_start']))/(60*60);
}
$hour = floor($duration);
$minute = ($duration - $hour)*60;
echo $hour.":".$minute;
Or better, let MySQL do the calculation:
SELECT (SUM(time_end) - SUM(time_start)) / 3600 FROM logbook WHERE date = '$date';
Related
I want to compare two dates with each other to see if 10 minutes has passed.
This is the code that I've got but I can't quite figure out how to do it.
I get the first date from my table (which is saved as a timestamp, example: 2017-03-26 22:33:45) and then I want to compare it with the time that is right now.
$sql = "SELECT saved_time from table1 where email = '$email'";
$stmt = $conn->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch()) {
$savedTime = $row[0];
}
$now = time();
if (/*10 minutes has passed between saved time and now*/) {
echo "Your account is unlocked";
} else if (/*10 minutes hasn't passed*/) {
echo "Your account is locked";
}
Try This code:
$timezone = "Asia/Kolkata";// Select Timezone as of your Preference or MySQL Server Timezone
date_default_timezone_set($timezone);
$sql = "SELECT saved_time from table1 where email = :email";
$stmt = $conn->prepare($sql);
$stmt->execute(array(':email' => $email));
while($row = $stmt->fetch()) {
$savedTime = $row[0];
}
// Uncomment below Line if $savedTime is in MySQL DATETIME Format
// $savedTime = strtotime($savedTime);
$now = time();
if (round(($now - $savedTime) / 60,2) >= 10){
echo "Your account is unlocked";
} elseif (round(($now - $savedTime) / 60,2) < 10){
echo "Your account is locked";
}
if $now = time(); #(this will generate a timestamp)
assuming $savedTime = $row[0]; is a MySQL date field, you can do
$savedTimeTimestamp= date("Y-m-d H:i:s", $savedTime);
...now 10 time 60 seconds is 10 minutes, so
if ($now > ($savedTimeTimestamp+600))
should do the job
If mysql solution is fine for you, then
select * from table T where TIMESTAMPDIFF(MINUTE,.saved_time,NOW()) > 10
If you need it in php, then
$db_time = $row['saved_time'];//as you have stored in timestamp already
$curr_time = strtotime(date('Y-m-d H:i:s'));
$diff = round(abs($db_time - $curr_time) / 60,2);
if($diff > 10)
Update
select now() as curr_time, saved_time from table where COND;
then in php
$diff = $curr_time-$saved_time;
if($diff > (10*60 ))
I am not sure whether this will work. But just an idea.
I'm am writing a PHP program that will get all of the dates for the current work week (excluding Monday). My code is as follows:
//Get Year
$sql = "SELECT YEAR(CURDATE()) AS CurYear";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$curyear = $row['CurYear'];
//Get Week
$sql = "SELECT WEEK(CURDATE()) AS CurWeek";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$curweek = $row['CurWeek'];
//Get Date of Tuesday
$sql = "SELECT STR_TO_DATE('$curyear$curweek Tuesday', '%X%V %W') AS TueDate";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$tuedate = $row['TueDate'];
It works and will return the date of this Tuesday, but is there a more efficient way of doing this, and moreover doing it for the following 3 days?
As explained here: How to find the day of week from a date using PHP?
you can use the date command:
$time = time(); // present time
$day = 3; //the day of the week we are looking for 0: sunday, 1 monday and so on
$dayofweek = date('w', $time); //current day week number
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', $time));
I'm not sure if you have a requirement of using MySQL, but I would do this in PHP directly. Below is a sample of how you could approach it. Once the variable $TuesTS is set, you can figure out the next day by adding (24*60*60) or 86400 to it, which is the number of seconds in a day.
<?php
$ts = time();
$todayNum = date('N');
if ( ($todayNum >= 2) && ($todayNum<=5)){
$offset = $todayNum - 2; // number of days after Tuesday;
$TuesTS = $ts-($offset*24*60*60);
echo "Tuesday : ".date('Y-m-d', $TuesTS);
}
?>
Database:
pub_id pub_name time ip
1 King 1359500087 3388636152
2 Queen 1359550082 6385394932
3 Jack 1359502084 5648646562
4 Heart 1359524083 9283834142
5 Jim 1359503082 3388636152
|_ Using time() |_ Using ip2long()
PHP/MySQLi Code:
$pub_id = $_GET['pub_id'];
$pub_name = $_GET['pub_name'];
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$time = time();
$query = $db->query("SELECT * FROM impressions WHERE pub_id = '$pub_id' AND pub_name = '$pub_name' AND ip = '$ip' AND time >= '???'");
$ip_adderss = $query->num_rows;
$query->close();
if($ip_adderss == 0){
// Redirect
} else{
// Do nothing
}
Now using the time() and IP how can i check that the current visitor last visit was 24 hour ago or not?
A simple solution is using if statement to check the ip time and now time.
// Check if the IP is new or returning
$query = $db->query("SELECT * FROM impressions WHERE pub_id = '$pub_id' AND pub_tag = '$pub_tag' AND month = '$month' AND day = '$day' AND ip = '$ip' AND domain = '$domain' AND valid = '1' ORDER BY id DESC");
$ip_address = $query->num_rows;
if($ip_address == 0){
$ip_ok = 1;
} elseif($ip_address != 0){
$impression = $query->fetch_assoc();
if($time >= ($impression['time'] + 86400)){
$ip_ok = 1;
} else{
$ip_ok = 0;
}
} else{
$ip_ok = 0;
}
Your time field contains unix timestamp, which is just a number of seconds.
So, it's easy to calculate a point that lies a day ago - just subtract 86400 from the current timestamp.
So, your condition is going to be as simple as an elementary school math:
WHERE time >= unix_timestamp() - 86400
or, if you want it using time() PHP function
$time = time() - 86400;
$query = "SELECT * FROM impressions WHERE ... AND time >= ?");
and then bind that value.
My code looks like this
$checksql= "SELECT Body, ( NOW() - Updated ) as Age FROM ".$wpdb->prefix."amazoncache WHERE URL = '" . $keyurl . "' AND NOT( Body LIKE '%AccountLimitExceeded%') AND NOT( Body LIKE '%SignatureDoesNotMatch%') AND NOT( Body LIKE '%InvalidParameterValue%');";
$result = $wpdb->get_results($checksql);
if (count($result) > 0){
if ($result[0]->Age <= 6001 && $result[0]->Body != ''){ //that would be 60 min 1 seconds on MYSQL value
$pxml = GetXMLTree($result[0]->Body);
return $pxml;
}}
I would like to change value 6001 to 1 week. Can anyone tell me how to calculate the value for 1 week, 2 weeks and 1 month?
So like this..
$var = 3601; // (60 * 60) + 1
$oneWeek = $var+strtotime("+1 week");
$twoWeek = $var+strtotime("+2 week");
$oneMonth = $var+strtotime("+1 month");
Am I in the ballpark?
I was wondering what is the best way to write the where statement in PHP where targetDate < Date.Now - HardCodedHours in PHP
If you mean how to do it in an MySQL query:
SELECT * FROM table WHERE targetDate <= date_sub(now(), interval 1 hour);
This will pull "field1" from table "myTable" where a DATETIME column "targetDate" is older than 12 hours.
$hardcodedHours = 12;
$sql = "SELECT field1 FROM myTable WHERE targetDate <= '" . date('Y-m-d H:i:s', strtotime("-$hardcodedHours hours")) . "'";
$result = mysql_query($sql);
$limitTime = time() - $nbHours * 3600;
$query = "SELECT ... WHERE TIMESTAMP(targetDate) < $limitTime;";
Or something like that.