Subtracting predefined time in PHP - php

I am having table time in mysql database with one attribute of type "TIME" which contains default value "09:00:00". what I am trying to do is to get this value and subtract it from current time.
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time);
However it never worked the way I need. result always like 00:00:00
what i want to achieve is something like:
$start_time = 09:00:00
$time_now = 09:34:23
so $delay should be 00:34:23.
any help to achieve that?
Thanks in advance.

Just a small bug in your code.... look below
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time); //BUG! String minus timestamp here...
Fix that second block with:
$start_time = strtotime($s['start_time']);
$delay = date( "H:i:s", time() - $start_time );
Agree with others though, this is really cleanly done on the database side as well.
Hope this helps

Related

Time Difference In Hour Minutes Seconds Php

Am using formula like this
<?php
//Coming From Mobile Phone
$Currenttime = 12:40:00;
//Server time Is Current Time of various timezone like(America/Chicago,America/Denver..)
$ServerTime = 12:55:00;
//Scheduletime is coming from database
$ScheduleTime = 14:45:00;
$ExactTime = ($Currenttime - $ServerTime) + $ScheduleTime
?>
I need Exact Time is like this 15:00:00
You should use the DateTime class for this:
$Currenttime = DateTime::createFromFormat('H:i:s', "12:40:00");
$ServerTime = DateTime::createFromFormat('H:i:s', "12:55:00");
$ScheduleTime = DateTime::createFromFormat('H:i:s', "14:45:00");
$ExactTime = $ScheduleTime->add($Currenttime->diff($ServerTime));

Calculating time difference between two 24H time in php

I'm struggling to calculate time difference between two 24 hour format. The data extracted from my database is like this:
$timein = "09:00";
$timeout = "17:00";
$timediff = $timein - $timeout
As above, how do I get the $timediff = 8?
$timediff = date("H",strtotime($timein) - strtotime($timeout));
You can also change to format of the result,
check: http://php.net/manual/en/function.date.php
If you first set them to full dates:
$timein = "09:00";
$timeout = "17:00";
$datein = strtotime(date("Y-m-d ".$timein);
$dateout = strtotime(date("Y-m-d ".$timeout);
So, in fact the date you are using (todays date) can be any other date given since it's all about the time between the hours. Then you can create the php date function:
$hourIn = date("G", $datein); // = 9
$hourOut = date("G", $dateout); // = 17
Followed by simple math:
$diff = $hourOut - $hourIn; // = 8
You can use strtotime() for time calculation.
$timein = strtotime('09:00');
$timeout = strtotime('17:00');
$timediff = $timein - $timeout;
echo 'Time: '.date('H:i:s', $timediff);
Break the problem down into two steps.
Convert each time into a number of seconds since midnight.
Calculate the difference between two times.
This breaks the larger problem down into basic problems which can be solved more easily. It is a very valuable tool in the programmer's belt.
For the second part, be sure to handle midnight the way you need to for your data. Perhaps if the timeout is before the timein, then you'll need to add 24 hours to the total? That's just a guess, because I do not know your data.

How to use a crontab to edit variables within a PHP file?

Is it possible to edit variables from a PHP file with the use of Cronjob? If so, how would I go about doing this?
Basically I have a PHP file that looks like this:
<?php
$daynumber = "1";
$txid = tx1;
$endtime = "2014-3-28 20:30:00 GMT+11:00";
?>
What I want is that every 24 hours it changes it by increasing the day number by one and the txid by one.
So basicaly after the cron job running 24 hours after the code above it will look like this:
<?php
$daynumber = "2";
$txid = tx2;
$endtime = "2014-3-29 20:30:00 GMT+11:00";
?>
Is it possible to do? If not, what other way could I produce the same result.
Thank you very much, I appreciate any help I receive.
Forget about cron for this task
Even without the database - if for some reason you don't want to use it:
<?php
define("FIRST_DAY_STRING", "2014-3-26");
define("SHIFT_DAYS", 'P2D');
define("TIME_SUFFIX", " 20:30:00 GMT+11:00");
$today = new DateTime();
$first_day = new DateTime(FIRST_DAY_STRING);
$interval = $first_day->diff($today);
$days = $interval->format('%R%a days');
$end_date = $today->add(new DateInterval(SHIFT_DAYS));
$day_number = intval($days) + 1;
$txid = "tx$day_number";
$end_time = $end_date->format('Y-n-j')
$end_time .= TIME_SUFFIX
?>
This example assumes that you start counting days from 2014-3-26 (day 1) and the endtime is always 2 days later at 20:30:00. You can alter the constants to get a different behavior.

Date is not formatting time correctly PHP

Hello I try to take the difference between two dates and display it.
My problem is that the time difference I get is not the correct one.
This is my code:
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
echo date('H:i', $diffTime);
The result I get is:
02:05
The currect time should be this:
00:05
My guess that the date somehow takes timezone or something like this but Im not sure.
Thanks.
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
So what you're actually doing here is generating two UNIX timestamps (numbers) and then subtracting them. then you're passing the resulting number as if it were still a timestamp to date().
essentially $diffTime is the number of seconds between your two times. you could divide by 60 to get minutes, and so on and so forth, but PHPs DateTime objects are much better.
From the PHP docs:
http://pl1.php.net/strtotime
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
try this
<?php
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
echo round(abs($time1 - $time2) / 60,2). " minute"
?>
Below is the solution of date time in years,days.hours,minutes and seconds.
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
$y = ($diffTime/(60*60*24*365));
$d = ($diffTime/(60*60*24))%365;
$h = ($diffTime/(60*60))%24;
$m = ($diffTime/60)%60;
$s = ($diffTime)%60;
echo "Minutes - " .$m;
echo "<br/>";

Why do I get unexpected hour when substracting two times

I have this simple function to subtract time: the input values are:
$current = '23:48:32';
$arrival = '23:41:48';
$time = date( "H:i:s", strtotime($current) - strtotime($arrival));
$waitingTime = $time; // 21:06:44
Looks like the diff for the minutes is correct, I am not sure why I am getting the 21 in front of the minutes. It should be 00:06:44.
Any help is appreciated.
Thank you.
try using gmdate()
$time = gmdate( "H:i:s", strtotime($current) - strtotime($arrival));
You can't expect this code to give you an interval.
the strtotime($current) - strtotime($arrival) line calculates a interval in seconds but when you pass it to date it assumes your speaking of an interval since epoch. so you get timezone translated value for $time; you must have gotten 9 because your probably behind UTC
use strtotime($current) - strtotime($arrival) / 3600 for hours and remainder divide by 60 for minutes. and then seconds
That's why PHP has DateTime & DateIntervals:
<?php
header('Content-Type: text/plain; charset=utf-8');
$current = '23:48:32';
$arrival = '23:41:48';
$current = DateTime::createFromFormat('H:i:s', $current);
$arrival = DateTime::createFromFormat('H:i:s', $arrival);
$diff = $current->diff($arrival);
unset($current, $arrival);
echo $diff->format('%H:%I:%S');
?>
Output:
00:06:44
This code echo 00:06:44!
$current='23:48:32';
$arrival='23:41:48';
$time = date( "H:i:s", strtotime($current) - strtotime($arrival));
echo $time;//00:06:44
What exactly is your problem?

Categories