MySQL time difference between now() and updated time - php

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?

Related

Storing MySQL Field Datetime in PHP Variable and Adding 1 Hour To It

Using MySQLi and PHP I have a Query Like below and I need to store the orderdate field from MySQL newOrder table (with format of Datetime like 2014-06-15 22:12:00) into a PHP variable like $orderTime and then add 1 Hour to the Time.
<?PHP
include 'conconfig.php';
$con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$query = "select * from newOrder WHERE orderdate < NOW() ORDER BY id DESC LIMIT 1";
if ($result = $con->query($query))
{
if ($result->num_rows > 0)
{}
else
{
$resultStr = 'Nothing found';
}}
}
$con->close();
is it possible something like inside the while loop?
$orderTime = $row['orderdate'];
$orderTime = $orderTime +1hour;
Thanks
Easily done with date() and strtotime():
$orderTime = date('Y-m-d H:i:s', strtotime($row['orderdate'] . '+ 1 hour'));
Update to answer question in comment:
To check if difference between $ordertime and current time is greater than 2 hours:
if (time() - strtotime($orderTime) > 7200) {
echo 'Difference is greater than 2 hours';
}

PHP/MySQL check for ip if availabe in past 24 hours using time()

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.

php cumulative time between time start and time end

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';

how to changes my variable from seconds to minutes

in my code below i subtract the previous date of the user john in the field name date_time
from the current date CURRENT_TIMESTAMP the variable $newdate echo the answer in seconds only how can i make it to display from seconds to minutes from minutes to hours then hours to days
$query1 = "
SELECT (CURRENT_TIMESTAMP - date_time) AS dnew, date_time
FROM user
WHERE name = 'john'
";
$result1 = mysql_query ($query1) or die('query error2');
$line = mysql_fetch_assoc($result1);
$newdate = $line[dnew];
echo "$newdate";
$query1 = "
SELECT (CURRENT_TIMESTAMP - date_time) * 60 AS dnew, date_time
FROM user
WHERE name = 'john'
";
use math ( * 60)

php and mysql. WHERE date <= X Hours ago?

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.

Categories