how to changes my variable from seconds to minutes - php

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)

Related

How do I update and set all rows in current hour only

I'd like to only change the rows from the current hour on todays date.
// Have the current date
$date = "2019-03-21 17:14:03";
$rel = 8 // like an id.
$sql = "UPDATE graph SET code = '$code' WHERE myDate = '$date' AND rel = '$rel'";
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
You can use the DATE_SUB function.
For example:
$sql = "UPDATE graph SET code = '$code' WHERE myDate >= DATE_SUB(NOW(), INTERVAL 1 HOUR)"
This will return all values where myDate is in the past hour.
Use DATE_FORMAT to compute the beginning of the current hour, and compare it against your datetime column. You don't need to pass the date as a parameter, you can get the current date/time with NOW():
UPDATE graph SET code = :code
WHERE myDate >= DATE_FORMAT(NOW(), '%Y-%m-%d %h:00:00') AND rel = :rel
You can do it in this way by ultilising HOUR() function in MYSQL:
$sql = "UPDATE graph SET CODE = '$code' WHERE DATE(myDate) = '$date' AND HOUR(NOW()) = HOUR ('$date') AND rel = '$rel'"

mysql check if uid record exist for today based on timestamp else do an insert

Im trying to do a mysql check if a record from $uid exist from today based on $timestamp and if it doesnt then do an INSERT.
//EXAMPLE RECORD FROM TABLE VOTE
--- #vote_fb_uid# --- #vote_time#
665414807 1369219044
tjt
//STEP 1 - do a look up on $uid and check with timestamp $today
$timestamp = $this->time;
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$sql = "
SELECT * FROM vote WHERE
vote_fb_uid = '$this->fb_uid',
WHERE vote_time = '$CHECK_IF_THERE_IS_AN_ENTRY_FROM_TODAY'";
$res = mysql_query($sql) or die( mysql_error());
//STEP 2 - If no records are found for today - then we do an INSERT
if($no_record_for_today) {
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
}
Obviously im strugling with the SQL part for the look up - im wondering if there isnt some in-built SQL function to do this or similar?
to check if you had a vote in the last 24 hours :
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND FROM_UNIXTIME(vote_time) >= DATE_SUB(NOW(), INTERVAL 1 DAY)
if you want to limit to the same day (mean you are allowed to post at 2013.05.21 23:55 and 2013.05.22 00:05)
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND DATE(FROM_UNIXTIME(vote_time)) = DATE(NOW())
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
mysql> SELECT CURDATE();
-> '2008-06-13'
mysql> SELECT CURDATE() + 0;
-> 20080613
Try this:
$today = date('Y-m-d'); //change it to timestamp if you want in timestamp
$sql = "
SELECT count(*) as total FROM vote WHERE
vote_fb_uid = '$this->fb_uid' and
vote_time = '$today'";
$res = mysql_query($sql) or die( mysql_error());
if($res[0]['total'] < 1){
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
} else{
//return error("custom","","Already Inserted.");
echo "already inserted";
}
Your $sql query have a syntax error, you have used two times clause WHERE the correct syntax to use two or more clauses in where is using AND to join them, to get only record wich don't have an entry for today you can use DATE_SUB with 1 day interval
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid',
AND vote_time <= DATE_SUB(vote_time, INTERVAL 1 DAY)

PHP / MySQL query - if current time/date is in schedule

I'm trying to develop a simple php based schedule ...
This is my database table:
Let's say that current time/date is: 2012-03-21 02:00:00
PHP script should echo: Meeting 2
I have a part of the script, but no idea what to do next
<?php
$con = mysql_connect("localhost","root","");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("schedule", $con);
$current_time = date("Y-m-d H:i:s");
$result = mysql_query("SELECT * FROM events");
while($row = mysql_fetch_array($result)){
echo $row['Subject'];
}
mysql_close($con);
?>
How to filter query, to mach current time and scheduled subject? Thank you!
(if current_time is between StartTime and EndTime - echo it's Subject)
Try this query
SELECT * FROM events WHERE NOW() BETWEEN StartTime AND EndTime
Try select * from events where StartTime <= NOW() and EndTime >= NOW()
NOW() will give you current time in mysql.
Please try the following query
select * from events where current_time between startTime and endTime
Your query should like this
$result = mysql_query("SELECT * FROM events WHERE current_time between startTime and endTime);
The mysql BETWEEN operator may be of some use.
$startDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:00:00');
$endDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:23:59');
$sql = 'select subject from events
where startTime between "'.gmdate("Y/m/d H:i:s", $startDate) .
'" and "' . gmdate("Y/m/d H:i:s", $endDate).'") ';
Firstly, you're going to have to convert your StartTime and EndTime into a timestamp using strtotime so you can compare it to the current time (same thing goes for current time):
http://php.net/manual/en/function.strtotime.php
Then just say if current_time > StartTime AND current_time < EndTime echo Subject.
Something like this should work:
while($row = mysql_fetch_array($result)){
$startTime = strtotime($row['StartTime']);
$endTime = strtotime($row['StartTime']);
if (strtotime($current_time) > $startTime && strtotime($current_time) < $endTime
echo $row['Subject'];
}
putenv("TZ=Asia/Calcutta"); // To Set TimeZone
$now = date("Y-m-d H:i:s");
$sql = "select * from events where '$now' between StartTime and EndTime";

24 hours of values

I have a sql table : date (Y-m-d) / time (00:00:00) / power (INT)
When I select a date from an inline datepicker, I am trying to post 3 HighCharts graph (one-24 hours, two-31 days of month, three-12 months of year) and I need to get the values out of the table for the chart to be created.
For the day, I need the 24 values for each hour '100,200,300,200,300 etc..'
Here is the PHP for the "day" but it is not working...
<?php
$choice = (isset($_POST['choice']))
? date("Y-m-d",strtotime($_POST['choice']))
: date("Y-m-d");
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("inverters", $con);
$sql = "SELECT HOUR(time), COUNT(power)
FROM feed
WHERE time = DATE_SUB('".$choice."', INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY HOUR(time)";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
echo $row['choice'].'<br />';
?>
This has been confirmed by another individual that the code does not work, would anyone have a helpful solution to fix the error ?
Alan
At the moment, your SELECT gives you only the results which happened exactly 24 hours before the current moment. What you need is a range. Example for 1 hour (indentation added for clarity):
WHERE `time` BETWEEN
DATE_SUB('".$choice."', INTERVAL 24 HOUR)
AND DATE_SUB('".$choice."', INTERVAL 23 HOUR)
This way, you'll get results with time in the 1-hour range of "now - 24 hours" and "now - 23 hours". The BETWEEN operator is equivalent to this:
WHERE `time` >= DATE_SUB('".$choice."', INTERVAL 24 HOUR)
AND `time` <= DATE_SUB('".$choice."', INTERVAL 23 HOUR)
You need a loop to walk over the rows:
$sql = "
SELECT HOUR(time) as h, power
FROM feed
WHERE date = '".$choice."'
ORDER BY HOUR(time)";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo $row['h'].":".$row['power']'<br />';
}
This will give you the power per day for a given month:
$sql = "
SELECT DAY(date) as d, SUM(power) as powerday
FROM feed
WHERE MONTH(date) = '".$month."'
GROUP BY DAY(date)
ORDER BY DAY(date)";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo $row['d'].":".$row['powerday']'<br />';
}
Thank you everyone for all your help !
The problem was in the first string, I only had to change the date format in addition to your wonderful examles !
$choice = (isset($_POST['choice'])) ? date("m",strtotime($_POST['choice'])) : date("m");
Thank You Very Much !
Alan

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