Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to time in php

Tags:

date

php

How to convert "2011-11-03T17:27:56Z" to time in php.

I want time difference from current time.

i.e. if time difference from current time is 10 minutes the I want 10 minutes. If its 1 day then I want 1 day.

like image 964
Muhammad Imran Tariq Avatar asked Nov 11 '11 17:11

Muhammad Imran Tariq


People also ask

How do you convert string to time?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

What does Strtotime mean in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How can I get current date and time in PHP?

echo "The time is " . date("h:i:sa"); ?> Note that the PHP date() function will return the current date/time of the server!

What is a timestamp in PHP?

Description ¶ time(): int. Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.


3 Answers

This little snippet will give you the difference in seconds between now and the given date.

$dateString = "2011-11-03T17:27:56Z";
$date = strtotime($dateString);
$diff = time() - $date;
echo $diff;

To give it the specific format you're asking for you can use the below function I found here:

function time_diff($s) { 
    $m = 0; $hr = 0; $d = 0; $td = "now";
    if ($s > 59) { 
        $m = (int)($s/60); 
        $s = $s-($m*60); // sec left over 
        $td = "$m min"; 
    } 
    if ($m > 59) { 
        $hr = (int)($m / 60); 
        $m = $m - ($hr*60); // min left over 
        $td = "$hr hr"; 
        if ($hr > 1) {
            $td .= "s";
        }
        if ($m > 0) {
            $td .= ", $m min";
        }
    } 
    if ($hr > 23) { 
        $d = (int) ($hr / 24); 
        $hr = $hr-($d*24); // hr left over 
        $td = "$d day"; 
        if ($d > 1) {
            $td .= "s";
        }
        if ($d < 3) { 
            if ($hr > 0) {
                $td .= ", $hr hr";
            }
            if ($hr > 1) {
                $td .= "s";
            }
        } 
    } 
    return $td; 
} 

Combining the both this is what you get:

$dateString = "2011-11-03T17:27:56Z";
$date = strtotime($dateString);
$diff = time() - $date;
echo time_diff($diff);

Outputs:

8 days

like image 163
Marcus Avatar answered Sep 18 '22 22:09

Marcus


$diffInSecs = time() - strtotime('2011-11-03T17:27:56Z');
like image 29
Moe Sweet Avatar answered Sep 19 '22 22:09

Moe Sweet


Working example: (codepad here)

<?php

$time_str = "2011-11-03T17:27:56Z";

//echo date('d/m/y H:i:s', strtotime($time_str));
$diff = abs(strtotime("now") - strtotime($time_str)); 

$years   = floor($diff / (365*60*60*24)); 
$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); 
$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); 
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); 
printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);

(time diff took here: How to calculate the difference between two dates using PHP?)

like image 38
Benjamin Crouzier Avatar answered Sep 18 '22 22:09

Benjamin Crouzier