Convert String to time in PHP [duplicate] - php

This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Closed 8 years ago.
I get the string data from the database
The data format like:
07:00:00.0000000
It means 7:00 am.
How can i convert this string into Date type for comparison.
Therefore, I can get 2 data. Such as
Start:
07:00:00.0000000
End:
16:30:00.0000000
After the time comparison, i can got a answer like 9.3.

You can use strtotime :
$start = strtotime("07:00:00.0000000");
$end = strtotime("16:30:00.0000000");
$difference = $end - $start;
Or with DataTime object :
$start = new DateTime("07:00:00.0000000");
$end = new DateTime("16:30:00.0000000");
$interval = $start->diff($end);
$difference = $end->getTimestamp() - $start->getTimestamp();
Then echo the result :
echo $difference; // in seconds
echo $difference / 60; // in minutes
echo $difference / 3600; // in hours
echo $interval->format('%s') // in seconds
It's then up to your preference. I also suggest you to have a look at this post regarding the performances of the two solutions.

Related

Datediff round returns zero [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 months ago.
I'm trying to run a datediff. I essentially want to say the number of days between 1st November 2022 and 1st November 2022 is ONE day.
When I attempt this, $number_of_days returned is 0. Can someone explain why this is and how to resolve?
$end = "2022-11-01";
$start = "2022-11-01";
$datediff = ($end - $start) + 1; // tried to fix
echo $datediff;
echo '<hr>';
echo ($datediff / (60 * 60 * 24));
$number_of_days = round($datediff / (60 * 60 * 24));
echo '<hr>';
echo $number_of_days;
The dates you shown in the code are strings, they are not numerically "minorable". You need to convert them into a int of time first like this:
$end = strtotime("2022-11-01");
$start = strtotime("2022-11-01");
$datediff = (($end - $start)/60/60/24) + 1;
Why doesn't it work: If you try to subtract strings like this, PHP will auto-convert the strings into the boolean value true and then convert them into the integer 1. You divided it by (60 * 60 * 24) which results in a very small number 1.1574074074074E-5 which then be rounded into 0.
Your subtraction is using string values, not date values, so actually ends up as being 2022 - 2022, which equals zero...
(newer versions of PHP will complain "Notice: A non well formed numeric value encountered")
You could convert to instances of DateTime, and then determine the difference that way:
$end = "2022-11-01";
$start = "2022-11-01";
// convert to DateTime
$dtEnd = DateTime::createFromFormat('!Y-m-d', $end);
$dtStart = DateTime::createFromFormat('!Y-m-d', $start);
// get the difference in days
$days = $dtEnd->diff($dtStart)->format('%a') + 1;

Add string variable (minutes) to time variable in php [duplicate]

This question already has answers here:
PHP Adding 15 minutes to Time value
(9 answers)
Closed 3 years ago.
I got some problems handling string and time.
I am reading a form which gives me a string like this: "08:00"
Now i am running a foreach loop after which i want to add e.g. 15 minutes to the upper string.
I tried to convert the "08:00" to a time with
$string = "08:00";
$time = date("H:i", strtotime($string));
echo $time; //echos 1577260800
How can i add e.g. 15 minutes or even better a string like $add = "10" to the $time? The following doesnt work.
$add = "10";
$newtime = $time + strtotime($add);
Just add time in seconds to an existing time.
$string = "08:00";
$timeInSeconds = strtotime($string) + 15*60; // 15*60 => 15 minutes in seconds
$time = date("H:i", $timeInSeconds );
echo $time; // shows 8:15
You can just use the strtotime's parsing of words.
Meaning you can just as you ask "add 10 minutes".
$string = "08:00";
$time = date("H:i", strtotime($string . " +10 minutes"));
echo $time; //8:10

How to sum up time in php? [duplicate]

This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;

strtotime() seems bugged with a special date [duplicate]

This question already has answers here:
How do I add 24 hours to a unix timestamp in php?
(7 answers)
Closed 8 years ago.
I'm writing a function which calculates the days between two dates.
Strangely I have a strange behaviour for one special day. Here is a part of my code:
$startTimestamp = strtotime('25-10-2014');
$endTimestamp = strtotime('28-10-2014');
for($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24))
{
echo date("d-m-Y", $i).'<br />';
}
This example gives me this result:
25-10-2014
26-10-2014
26-10-2014
27-10-2014
I don't know why the date "26-10-2014" appears two times.
If I try with another dates like:
$startTimestamp = strtotime('25-11-2014');
$endTimestamp = strtotime('28-11-2014');
The result is correct:
25-11-2014
26-11-2014
27-11-2014
28-11-2014
It is probably because of the DST (daylight saving time) in the timezone your PHP is configured to.
This mean, if you jump forward 24h, you'll still be the same day, because the day is 25h long.
I would use the DateTime class:
$startTime = new DateTime('25-10-2014');
$endTime = new DateTime('28-10-2014');
do {
echo $startTime->format('d-m-Y'), PHP_EOL;
} while($startTime->modify('+1 day') <= $endTime);
Note that you can use the <= operator when comparing DateTime objects. Also the DateTime::modify() method is aware of daylight saving time issues. (Note that there is a change in daylight saving time on Oct 26)

Difference of hours between two dates (integer) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate number of hours between 2 dates in PHP
How can I get the number of hours of difference between two dates in PHP? I need to get an integer since I want to know if is bigger or smaller than a particular value.
Try:
$date1 = "2012-11-05 12:35:00";
$date2 = "2012-11-07 14:35:00";
$diff = strtotime($date2) - strtotime($date1);
$diff_in_hrs = $diff/3600;
print_r($diff_in_hrs);
Manual
Demo
If you have an up-to-date PHP
$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');
// Procedurally
$interval = date_diff($dateOne, $dateTwo);
// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);
See: http://www.php.net/manual/en/class.dateinterval.php
<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>

Categories