MySQL table "flightSched" is connected to time, similar to the one below:
flightNo |day |time |arrivalTimeSec
=============================================
WERE112 |Tuesday | 1:00 |1381186800
FGHG234 |Tuesday |23:00 |1381266000
CGHF345 |Tuesday |00:00 |1381183200
I have a mysql query that select all data between two times. This is the query:
$CurrentTimeMinus30min = date('H:i', strtotime('-30 minutes')); //Current Time minus 30minutes
$CurrentTimeMinus30min = strtotime($CurrentTimeMinus30min);
$CurrentTimePlus4Hours = date('H:i', strtotime('+240 minutes')); //Current Time plus 4 hours
$CurrentTimePlus4Hours = strtotime($CurrentTimePlus4Hours);
$query = $mysqli->query("
SELECT * FROM flightSched
WHERE day = '$currentDay'
AND arrivalTimeSec
BETWEEN '$CurrentTimeMinus30min'
AND '$CurrentTimePlus4Hours'
");
I was advised to used strtotime() function on the time values to be able to use them in a BETWEEN MySQL query. This doesn't seem to be working at all.
Where am I going wrong with this query? Any help will be appreciated.
today I found the same problem with yours (mine about coordinates).
and I found out that in some case, a BETWEEN operator can only be used like this
..... WHERE columname BETWEEN smallervalue AND biggervalue
previously I've tried with the biggervalue at front since I dealt with negative numbers, and it fails.
you might found the same problem with your timestamp.
strtotime returns a timestamp so passing that into the MySQL query, like above, won't work. Try using FROM_UNIXTIME instead.
$query = $mysqli->query("SELECT * FROM flightSched
WHERE day = '$currentDay'
AND FROM_UNIXTIME(arrivalTimeSec) BETWEEN FROM_UNIXTIME($CurrentTimeMinus30min) AND FROM_UNIXTIME($CurrentTimePlus4Hours) " );
EDIT - I hadn't noticed that arrivalTimeSec was also a timestamp. The above mightn't be a workable answer for you, but try it. If it doesn't work, as others say, define what you mean by
This doesn't seem to be working at all.
Is it not returning any rows? Is it returning an error? Can you print out $CurrentTimeMinus30min and $CurrentTimePlus4Hours? Narrow down the potential areas for problems.
Have you tried to encapsulate the between? This could potentially solve your problem:
SELECT * FROM flightSched
WHERE day = '$currentDay'
AND (arrivalTimeSec BETWEEN '$CurrentTimeMinus30min' AND '$CurrentTimePlus4Hours')
Also why not just do:
$CurrentTimeMinus30min = strtotime('-30 minutes');
Or
$CurrentTimeMinus30min = strtotime(date('Y-m-d H:i:00', strtotime('-30 minutes')));
Please send us some examples of what your variables are generating.
Your time calculation with date("H:i",...) and strtotime(..) seems to actually produce the correct results, although there is a much easier way to add/substract n minutes from the current time:
$now = time();
$currentTimeMinus30min = $now - 30*60; // 30 minutes * 60 seconds
$currentTimePlus4Hours = $now + 4*60*60; // 4 hours * 60 minutes * 60 seconds
(I assume your time entries in your database are unix timestamps.)
Your query looks fine, too, but there are a few things to keep in mind:
You have redundant fields in your database (day and time can be calculated from the timestamp)
Working with time variables can easily lead to confusion, as the time passes on and if you have no entries in your database that match the specified time range (-30m to +240m) the result set is empty. So to test the query update the database with current time stamps.
I would suggest the following:
Drop the redundant columns day and time and just use the timestamp as base for your calculations, because the day and time is already included in the timestamp. So just use a simple query like
select * from flightShed
where arrivalTime between $begin and $end
Related
I'm trying to find the difference between 2 times obtained from a sql query:
$WakeTime = '05:00:00'; //this is data of today pulled from sql.
$SleepTime = '20:30:00'; //this is data of yesterday pulled from sql.
The result I'm looking for is like:
$Sleep = 24 + $WakeTime - $SleepTime;
so that $Sleep should give '08:30';
Which it's the correct method to get this?
Try something like this, maybe it will be for you.
$result = gmdate("H:i", 86400 - strtotime($SleepTime) - strtotime($Waketime));
output: 08:30
$sleep = gmdate("H:i", 86400 - strtotime($row["SleepTime"]) + strtotime($row["WakeTime"]));
This worked consistently. The time, pulled from sql, formats 00:00 Hrs as 1584297000. So just in case the seconds are required by anyone, this is the number you need to subtract from your time. Probably this differs from TimeZone to TimeZone; you will need to check.
I have inherited an old system at work, php 4.4 and MySQL that we run our helpdesk software from, I cannot upgrade anything until next year.
I'm struggling with something though.
I need to show the total number of calls logged between 2 and 1 hour ago. in the database, the unix timestamp for each call logged is in the column "logdatex"
in my php I have the following
$OneHourAgo = strtotime('-1 hour'); //time 1 hour ago as Unix Timestamp
$TwoHoursAgo = strtotime('-400 hour'); // time 2 hours ago
$Test = mysql_query("select count(*) from opencall where logdatex between $OneHourAgo and $TwoHoursAgo") or die(mysql_error());
Now, in MySQL Query Browser, if I put in the query but replace the variables with the actual numbers (I did an echo to get the numbers) it works fine and returns the desired number:
select count(*) from opencall where logdatex between 1326767703 and
1386764103
(the above doesn't use a 1 hour sample, more like a few years) Please can you help me get the number in to a variable, I cannot figure out how to do this.
Any help appreciated
The mysql_query does not directly return the results of the query. Rather it returns a result resource. http://www.php.net/manual/en/function.mysql-query.php
So you will need to use mysql_fetch_row to get the results.
http://www.php.net/manual/en/function.mysql-fetch-row.php
$row = mysql_fetch_row($Test);
$count = $row[0];
If you have more than one row, you would loop until the mysql_fetch_row returns false. But since you know you are only going to get one row, you can do this.
You should change your query:
select count(*) from opencall where logdatex between $TwoHoursAgo and $OneHourAgo
Because http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between says, it should be between min and max.
I'm not sure to understand you but, may be that?
$OneHourAgo = strtotime('-1 hour'); //time 1 hour ago as Unix Timestamp
$TwoHoursAgo = strtotime('-2 hour'); // time 2 hours ago
A link: http://php.net/manual/es/function.strtotime.php
In my DB I have UNIX Timestamps as due dates for jobs that need to run. On a given day or a given hour I'd like to know which jobs need to run. I have the following code to query my jobs table:
$job = Model_Job::find('first',
array(
'where' => array(
array('status_id', 1), // active
array('due_date', today)
)))
The last array is just pseudo code of course. I came up with the following code to figure out if a timestamp is today.
date('Ymd', strtotime(Date::forge(time())->get_timestamp())) == date('Ymd', strtotime($job->due_date))
How would I combine that with my query? Is there a better way then looping through the queried array?
Now, this is probably not a good way but it was the first that came to mind.
$today = strtotime('today 00:00');
You can use the date from that snippet and set a WHERE due_date > $today I'm not quite sure how to do that with your database model.
edit: This relies on that you have correct timezone settings in your php configuration.
As sebastian pointed out, this will get all future dates also, but you could simply use the same technique to get the last timestamp of the day
$todaystart = strtotime('today 00:00'));
$todayend = strtotime('today 23:59:59'));
and the use a BETWEEN operator in your SQL-query
I'm wanting to display on a php page the difference between the current server time and a datetime row plus a row that has milliseconds in it, so I guess the equation would look kind of like ((Datetime+Milliseconds)-Server Time).
The only problem is, I'm not sure how to do it in code. I can currently get the difference between the datetime row and the current time with echo strtotime($row['date_added']) - time(); When I try adding the row that contains the milliseconds, date_mil, I get a really long number.
The date in the row date_added looks like 2012-05-25 16:55:06 and the value of the date_mil is around 218238.
I'm still learning how to do all of this, and this has me confused. Thanks for the help!
I just solved my own problem.
$difference = time() - strtotime($row['date_added']);
$milliseconds = round(($row['date_mil']) * .001);
echo $milliseconds - $difference;
I have a field in my DB that records the last time a check occured, I also have frequency of how often the checks should occur.
What I need is: I get the past timestamp and I need to add the frequency in minutes to it to check against current time stamp.
Can't think of a way via PHP, anyone could help?
#fredley commented with just the right answer:
timestamp += minutes * 60
If you still want a plain PHP variant of that why not try strtotime?
//$oldTimestamp;
$minutes = 15;
$newTimestamp = strtotime('+ ' . $minutes . ' minutes', $oldTimestamp);
Depending on your database configuration, and table layout, you may want to look at the MySQL ADDTIME function