Time format adding six hours - php

I’m having an issue calculating the difference between two time. Here’s my code.
<?php
$Now = date('h:i:s');
echo 'Now is: '. $Now . '<br>';
$time1 = strtotime('01:22:24');
$time2 = strtotime('01:28:24');
$diff = $time2 - $time1;
echo 'Time 1: '.date('h:i:s', $time1).'<br>';
echo 'Time 2: '.date('h:i:s', $time2).'<br>';
if($diff){echo 'Difference: '.date('h:i:s', $diff);
}else{echo 'No Difference.';}
?>
What it outputs is
Now is: 03:07:01
Time 1: 01:22:24
Time 2: 01:28:24
Difference: 06:06:00
The time “Now is:” is the correct time. The timezone in my php.ini is set to US/Central and I’ve even tried America/Chicago but no help. I’m running PHP 5.3.5 with apache 2.0
Any ideas? Thanks.

The date function takes a UNIX timestamp as its second argument. UNIX timestamps are seconds since January 1, 1970. So when you pass in $diff as the second argument to date, it is interpreting that as seconds since January 1, 1970 and then displaying the hour, minute and date components of that date!
You should look into using http://us3.php.net/manual/en/class.datetime.php and http://us3.php.net/manual/en/datetime.diff.php instead. Here is the code using these classes:
<?php
$time1 = new DateTime('01:22:24');
$time2 = new DateTime('01:28:24');
if ($time1 == $time2) {
echo 'No Difference.';
} else {
$diff = $time2->diff($time1);
echo 'Difference: ' . $diff->format('%H:%I:%S');
}

You're trying to output a difference that's expressed in seconds as a date value.
The difference should be expressed as:
if($diff){echo 'Difference: '.$diff . " seconds";

The value of $diff is a number of seconds. When you use date however you're localising the time which in this case is adding 6 hours.
Use gmdate instead.
http://uk1.php.net/gmdate
if($diff){echo 'Difference: '.gmdate('h:i:s', $diff);

Related

hh:mm:ss difference return wrong answer in php

I am using this code to calculate the time period. In fact, I want the period between two HH:MM:SS moment and have a result in HH:MM:SS format.
$time1 = strtotime('00:00:00');
$time2 = strtotime('00:00:07');
$diff = $time2 - $time1;
$diff = date('H:i:s', $diff);
I'm expecting 00:00:07 but I get 01:00:07. What could be the problem?
Irvin run here, and get right answer but I run the same code on my local machine and wrong!!
Is it timezone or maybe some configuration effect the result?!
Please stop using strtotime and date functions. Use the DateTime class.
And if you would have searched better, finding difference for two dates has already been all over SO. For instance, my answer here. Slightly changed it would look like:
$create_time = "00:00:00";
$current_time="00:00:07";
$dtCurrent = DateTime::createFromFormat('H:i:s', $current_time);
$dtCreate = DateTime::createFromFormat('H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);
echo $diff->format("%H:%I:%S"); // to get HH:MM:SS format
This returns 00:00:07
See DateInterval::format for more formatting details.
It's because when you minus time 1 from time 2 you end up with 7;
The date function needs a timestamp, 7 is not a timestamp.
You could use http://php.net/manual/en/datetime.diff.php to calculate the difference instead?
You should consider the UNIX epoch which cancels timezone diffrences in ini settings, essentially. it's Jan 1st '70. You can check your server/app's setup for timezone by adding e, O or P. Check this code:
$time1 = strtotime('00:00:00 +0000');
$time2 = strtotime('00:00:07 +0000');
$epoch = strtotime('01 Jan 1970');
$diff = $time2 - $time1;
var_dump($time1,$time2,$epoch,$diff);
$diff = date('H:i:s', $epoch+$diff);
echo $diff;

How to get d-m-y-h current time and calculator in php

i want get only day, month, year, hour not include minute, second and subtraction it 5 hours. I search but result include minute and second
Ex: now 13/10/2015 18h and i want time now - 5 hours is 13/10/2015 13h
$dateTime = new DateTime();
$dateTime->modify('-5 hours');
echo $dateTime->format('d/m/Y h\h');
Read more about DateTime class
echo date('d/m/Y G', strtotime('-5 hour')) . 'h';
Have a look to strftime() :
<?
$time=time(); // This is the time in second from the epoch
$time -= 3600 * 5; // I subtract 5 hour
echo strftime("%d/%m/%Y %H", $time); // I print the calculated time
?>

Calculate time difference between 2 date()'s

I've got an input field where I type in a deadline in words. For example, "5 minutes", "2 days", "6 weeks", "8 months". What I want for the program to do is to calculate how long it will take when that deadline ends. And also if that deadline is almost ending, for example if 80% of the given time has passed.
I was thinking something like that php splits the given time in seconds, and then checks how many minutes and hours or days fit in those seconds and then puts that in dateTime. Like current date + input = futureDate.
I know I probably shouldn't use percentages, it's just an example.
<input type="text" name="getFutureTime">
<?php
$futureTime = $_POST['getFutureTime'];
$dateNow = date('d-m-Y H:i:s');
if($futureTime > $dateNow){
//Calculate
echo "Deadline has passed";
}else if (($futureTime / 100 * 80) < $dateNow){
//Calculate
echo "Deadline is almost passed";
}
?>
Here the values are string and you cant compare this like that. Convert them to timestamp first. Try with -
$futureTime = strtotime($_POST['getFutureTime']);
$dateNow = time();
I have a solution with, caculate how date later :)..., copy this to localhost and run :) diff
$d1=new DateTime("now");
$d2=new DateTime($_POST['DateInput']);
$diff = $d1->diff($d2);
echo "<pre>";
print_r($diff);
echo "</pre>";
$date = $diff->format('%d'); //It date
$year = $diff->format('%y'); //It year
$month = $diff->format('%m'); //It month
echo $diff->format('%a'). "<br/>";
foreach($diff as $key => $value){
echo $key . "<br/>";
echo $value;
}
$value = strtotime('14/12/2012');
echo($value);
Update
$diff->format('%a') // Is date from now example 10/12/2015 - 10/5/2015 = 7
$diff->format('%a') is 7
Try this it will work:
$date_a = new DateTime('2015-05-07 13:03:48');
$date_b = new DateTime('2015-02-04 13:03:41');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%m Months %d Days %h Hours %i Minutes %s Seconds');

How to get difference of a time in seconds in PHP?

I have a database column with datatype "time" which stores 11:30:45. I have fetched this time in a variable say
$databasetime = 11:30:45
I want to declare a variable say $currenttime which will contain time just now. Like its 11:33:30 right now and another variable which will contain their difference in seconds like
$timediff = $currenttime - $databasetime;
echo $timediff;
I am trying $currenttime = time(); but I am not getting the result which I desire. I want $timediff = 165 but when I echo time(), I am getting a very big value.
$databasetime = strtotime('11:30:45');
$curtime = time();
echo $curtime - $databasetime;
You can do it in the following way:
$databasetime = '11:30:45';
$time1 = strtotime($databasetime);
$time2 = strtotime('now');
$diff = $time2 - $time1;
echo 'your difference: '.date('H:i:s', $diff);
$datetime1 = new DateTime('10:35:56 2013-11-17');
$datetime2 = new DateTime('10:35:50 2013-11-17');
$interval = $datetime1->diff($datetime2);
echo $interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";
<?php
$currentTime = time();
$futureDateTime = new DateTime('11:30:45'); // might want to specify a date and timezone, system TZ by default
$futureTime = $futureDateTime->format('U'); // get unix timestamp
$timeDiff = $futureTime-$currentTime;
?>
You use the DateTime 'OO' methods to return 'unix timestamp' integers directly
<?php
$now = new DateTime('now');
$date = new DateTime('11:30:45');
echo $now->getTimestamp() - $date->getTimestamp();
?>
Here is a simple example.
$databasetime = '11:30:45';
$timedif = abs(strtotime('now') - strtotime($databasetime));
echo $timedif; //echos the difference in seconds.
abs is used just to prevent neg numbers, which you may or may not want to do.
My problem is solved now. Actually all answers are right but the problem was due to the default time zone. I wanted Asia/Kolkata time zone but default European timezone apache was picking. That's why I was not getting my desired results.
So, I am finally using below code:
date_default_timezone_set('Asia/Kolkata');
$databasetime = strtotime('11:30:45');
$curtime = time();
echo $curtime - $databasetime;

One day added in gmdate

I'm trying to understand how gmdate works, i have the next simple code:
<?
$seconds = 86399;
echo gmdate("d \d\a\y\s H:i:s",$seconds);
?>
The result i was expecting is 0 days 23:59:59, but i get 1 days 23:59:59 why is returning one day?
I know that i can do something like this to avoid the problem:
<?
define("SECONDS_BY_DAY",86400);
$seconds = 86399;
echo floor($seconds / SECONDS_BY_DAY) . " days ";
echo gmdate("H:i:s",$seconds);
?>
But i want to understand why gmdate is returning one day instead of 0
The d format specifier does not print the number of whole days passed since the start of the Unix epoch; it prints the day of month.
Your $seconds value corresponds to 23:59:59 on January 1, 1970 -- hence d is 1.
To get the total number of days since the epoch start, use
$date = new DateTime('#'.$seconds);
$epoch = new DateTime("#0");
$diff = $date->diff($epoch);
echo $diff->days;

Categories