I show my problem:
I have two variables that contain the TIME variable from an MySQL database:
$row[0] //it contains for instance 16:30:00
$row[1] //it contains for instance 18:00:00
How do I find the term that is derived from their subtraction?
As they are already time variables I tried to do a subtraction directly with these values but I get integer values rounded down.
I tried existing questions but none foresaw a time in the format HH: MM: SS.
Thank you.
Use TIMEDIFF function instead of substrction:
SELECT REPLACE(TIMEDIFF(col1,col2),'-','')
REPLACE is used to ensure absolute result.
try to use
$query = select TIMEDIFF(field_name, '$input_date') from tbl_name where condition
at mysql
This question was already answerd... well
You need to convert it on timestamps
$time1 = strtotime($row[0]);
$time2 = strtotime($row[1]);
$diff = $time2 - $time1;
Also be aware if it use after 1 day, it will not show the "last day" difference... So use year month day...
Related
I have a mysql table events with column event_date varchar(10) which takes in dates in the form 7-5-2014.
I have an object which can give me:
// displays current month (5)
$m = $time->getMonth();
echo $m;
//displays current day (1)
$d = $time->getDay();
echo $d;
// displays current year (2014)
$y = $time->getYear();
echo $y;
I am trying to figure out a way to get events within the current day, month, and year.
The php query below is wrong but it gives you and idea as to what im trying to do:
$eventsWithinDay = query("SELECT * FROM events WHERE LEFT(event_date, 2)='%s'", $d);
How can I do this correctly?
Better yet, what is the most efficient way to do this?
I can make any necessary changes to the database or php.
Thanks in advance.
You an do it in various ways. For instance:
SELECT smth FROM somewhere WHERE datefield LIKE '2014-05-%'
OR
SELECT smth FROM somewhere WHERE datefield >= $start_interval and date <= $end_interval
In the latter case you'd have your date stored in a BIGINT column and use php's time() to set its value. Then using PHP's time functions (like strtotime('-1 day')) get timestamps for dates you wish to query against. So a simple example would be:
$q = "SELECT ... WHERE datefield >= " . strtotime('-2 days') // this would search for dates from at least 2 days ago
You could also use MYSQL's built-in date-time functions, but i have no experience with those so i'm not going to advice on something i don't know myself :)
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
I am trying to figure out how to subtract 1 hour form the time being outputted. Our web server is 1 hour ahead of our time. I am using the following to return the time a table was created in our MySQL database.
ini_set('date.timezone', 'America/Los_Angeles');
$con = mysql_connect('localhost','database','password');
$db = mysql_select_db('database');
$sql = mysql_query("SHOW TABLE STATUS WHERE `name`=\"initm\"");
$foo = mysql_fetch_array($sql);
$ts = strtotime($foo['Create_time']);
echo "<h3>Last Updated ";
echo date('m/d/Y g:i a T', $ts);
echo "</h3>";
If I try to subtract time $ts = strtotime($foo['Create_time']-3600);
it returns Last Updated 12/31/1969 4:00 pm PST. I understand it is subtracting from the UNIX timestamp, which is defined as the number of seconds since January 1, 1970 and not from the time in the table.
I tried adding ini_set('date.timezone', 'America/Los_Angeles'); but it just changes the time zone outputted.
Any advice is greatly appreciated. A novice in PHP and MySQL.
strtotime assumes it's parameter is a string, and converts it to seconds. You need to do the subtraction after the conversion:
$ts = strtotime($foo['Create_time'])-3600;
SELECT UNIX_TIMESTAMP(Create_time) - 3600 ...
is far easier than multiple round-trips through PHP's and MySQL's date/time processing systems.
However, since strtotime is giving you invalid results, I'm guessing that whatever you're storing in Create_time is not actually a native mysql data/time value, but probably some wonky non-standard date value as a string. That means strtotime() is returning a boolean FALSE to indicate failure, and then you force PHP to convert that false to an integer 0 to handle your "minus 3600" calculation.
You should generally always store things in native formats/types, to prevent occurences such as this. Native allows you to use native facilities for processing. Custom formats mean pain/suffering
Why not :
$query = "SET time_zone = '-6:00'";
This will set your configuration in the same way as SET charset utf8.
How would i turn
2012-04-11 12:49:14
into a unixtime stamp?
I have tried
$time = mktime("2012-04-11 12:59:14");
and
$time = strtotime("2012-04-11 12:59:14");
EDIT
Basically on update my database adds a date/time that looks like this.
2012-04-11 12:49:14
I need it to be turned into a unix timestamp so i can use a "time ago" function i found.
My tests have revealed,
Database input -> 2012-04-11 13:22:05 which is converted into -> 1334143355 -> But the current time from(time()) is ->1334146956
I dont see why they do not match up?
The statement $time = strtotime("2012-04-11 12:59:14"); is working just fine.
Do a
echo $time;
after your declaration.
It sounds like an issue with mismatched time offsets (daylight saving perhaps). It is always best to do it all in PHP or all in MySQL to avoid mismatched time offsets.
1334146484 - 1334142872 = 3612s = 1h 12s
You should use UNIX_TIMESTAMP either when inserting or retrieving the data depending on whether you prefer storing as unix timestamp or datetime -
INSERT INTO `table` (date_field) VALUES (UNIX_TIMESTAMP('2012-04-11 12:59:14'));
SELECT UNIX_TIMESTAMP(date_field) FROM `table`;
I expect that the difference is from being in a different timezone. The difference is more or less +1 hour form the expected result. You need to be more specific about what time you want - as in are you recording/retrieving time from your timezone, or from UTC.
$time = strtotime("2012-04-11 12:59:14"); is correct option.
Hey guys i'm trying to figure out how to subtract one time from another using php to get the amount of time left between the two times. So for example
time left = time1-time2
or
timeleft = 15:35-15:30
which would be equal to 5mins left.
Currently I am loading the two times like so.
time1 is coming from my database (which is the time we are waiting for, and in my case the time we are waiting for is the time for next update) and time2 is the current system time.
I tried using this code
$timeleft = $dbtime - $curtime;
$dbtime = time loaded from database.
$curtime = current system time.
But that just returns a 0.
Any help is appreciated thanks.
Use strtotime to turn the date string to unix timestamp.
$timeleft = strtotime($dbtime) - strtotime($curtime);
You have to convert both times into timestamp. One good function for that is the strtotime() http://php.net/manual/en/function.strtotime.php that try to convert a string into timestamp.
Then do your maths as you know and then just use the date() http://www.php.net/manual/en/function.date.php function to fomrat your time into anything you like
Use strtotime to convert strings to unix timestamps:
$timedifference = strtotime($dbtime) - strtotime($curtime); // or also
$timedifference = strtotime($dbtime) - time();
You negating one string from another - the result in 0 because (int)(string) = 0
Your must use like this
$dbtime = time();
// query
$timeleft = time() - $dbtime;
See also strtotime() function, if you date is parsed by it