Forum Moderators: coopster

Message Too Old, No Replies

Parse a date (in non std format) and show the time difference

         

flapane

1:25 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



Let's consider this text:
Wed Apr 07 13:01:45 +0000 2010


Any way to calculate and display a time difference between this and the present time?
If that text was displayed in unix time, it was easier...

Thanks

Readie

1:30 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could take mktime() [uk.php.net] away from time() [uk.php.net].

Will probably need to use regex to split the date up in it's current format, then a switch statement to get the month

flapane

1:39 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



I get what you mean, that could be interesting.
I'll do a couple of tests trying to split the date.
I already wrote the code to get the correct format AFTER this passage, some months ago.

Readie

1:57 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was intrigued enough that I decided to write this function myself.

<?php

function difference_in_time($input) {
if(preg_match('/^[a-z]+\s([a-z]+)\s([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})\s\+[0-9]{4}\s([0-9]{4})$/is', $input, $out)) {
if(strtolower($out[1]) === "jan") {
$month = 1;
} elseif(strtolower($out[1]) === "feb") {
$month = 2;
} elseif(strtolower($out[1]) === "mar") {
$month = 3;
} elseif(strtolower($out[1]) === "apr") {
$month = 4;
} elseif(strtolower($out[1]) === "may") {
$month = 5;
} elseif(strtolower($out[1]) === "jun") {
$month = 6;
} elseif(strtolower($out[1]) === "jul") {
$month = 7;
} elseif(strtolower($out[1]) === "aug") {
$month = 8;
} elseif(strtolower($out[1]) === "sep") {
$month = 9;
} elseif(strtolower($out[1]) === "oct") {
$month = 10;
} elseif(strtolower($out[1]) === "nov") {
$month = 11;
} elseif(strtolower($out[1]) === "dec") {
$month = 12;
} else {
$month_check = 1;
}
if(!isset($month_check) || $month_check !== 1) {
$old_time = mktime($out[3], $out[4], $out[5], $month, ltrim($out[2], "0"), $out[6]);
$output = (time() - $old_time);
} else {
$output = 'Invalid month format';
}
} else {
$output = 'Invalid input';
}
return $output;
}

$some_date = 'Wed Apr 07 13:01:45 +0000 2010';
echo difference_in_time($some_date);

?>

flapane

2:02 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



I was exactly at the "elseif ==" point, but I think I'll save my time, say you thanks, and use your function.
Thanks :)

flapane

2:17 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



I'm sure I'm missing something...
I was generalizing it using a generic $tempo2 I obtained from
$tempo2 = explode(" ", $tempo, 6);
with $tempo being Wed Apr 07 13:01:45 +0000 2010

$some_date = '$tempo2[0] $tempo2[1] $tempo2[2] $tempo2[3] $tempo2[4] $tempo2[5]';


but I get an Invalid Input

Readie

2:20 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$some_date = '$tempo2[0] $tempo2[1] $tempo2[2] $tempo2[3] $tempo2[4] $tempo2[5]';

Variables are not parsed within single quotes. Change to double quotes and it might work, though I prefer to concatonate my variables in personally.

flapane

2:26 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



Woops, noob error.
Thanks

flapane

3:42 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



I don't know where's the problem, but I get a 2hour (7200 in unix time) difference

BEFORE difference_in_time:
Wed Apr 07 14:59:20 +0000 2010

AFTER:
1270645160 ---> Wed, 07 Apr 2010 12:59:20 GMT

while I should get Wed, 07 Apr 2010 14:59:20 GMT

I solved by manually adding +7200, but that's kinda lame, I don't really like it :))
I'm sure that it must be related to daylight saving time or something similar.

Readie

3:48 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It might be your server's clock. Try adding the following line into the function, preferably outside of any of the if statements:

date_default_timezone_set(Europe/London);

flapane

4:02 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



Nothing changed

function difference_in_time($input) {
date_default_timezone_set(Europe/London);
[foo]


PHP 5 and GMT+2 (daylight) here.

Readie

4:06 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, if you're in GMT+2 you'll need to set the timezone to your local time. My apologies, but you used GMT as your timezone identifier above so I assumed you were based in the UK.

[php.net...]

flapane

4:10 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



My bad, I forgot to specify that I'm GMT+2

gmmktime seems to do the trick
I'm doing all the tests here, trying to build a Twitter parser:
[flapane.com...] using human_time_diff function "stolen" from Wordpress.

I only need to understand how to include replies to me (@flapane], set a cache time, and it will work.

<?
$username='flapane';
$format='json'; // format
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable


function difference_in_time($input) {
if(preg_match('/^[a-z]+\s([a-z]+)\s([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})\s\+[0-9]{4}\s([0-9]{4})$/is', $input, $out)) {
if(strtolower($out[1]) === "jan") {
$month = 1;
} elseif(strtolower($out[1]) === "feb") {
$month = 2;
} elseif(strtolower($out[1]) === "mar") {
$month = 3;
} elseif(strtolower($out[1]) === "apr") {
$month = 4;
} elseif(strtolower($out[1]) === "may") {
$month = 5;
} elseif(strtolower($out[1]) === "jun") {
$month = 6;
} elseif(strtolower($out[1]) === "jul") {
$month = 7;
} elseif(strtolower($out[1]) === "aug") {
$month = 8;
} elseif(strtolower($out[1]) === "sep") {
$month = 9;
} elseif(strtolower($out[1]) === "oct") {
$month = 10;
} elseif(strtolower($out[1]) === "nov") {
$month = 11;
} elseif(strtolower($out[1]) === "dec") {
$month = 12;
} else {
$month_check = 1;
}
if(!isset($month_check) || $month_check !== 1) {
$old_time = gmmktime($out[3], $out[4], $out[5], $month, ltrim($out[2], "0"), $out[6]);
$output = $old_time;
} else {
$output = 'Invalid month format';
}
} else {
$output = 'Invalid input';
}
return $output;
}

function human_time_diff( $from, $to = '' ) {
//function stolen from wordpress
if ( empty($to) )
$to = time();
$diff = (int) abs($to - $from);
if ($diff <= 3600) {
$mins = round($diff / 60);
if ($mins <= 1)
$since = '1 min';
else
$since = sprintf('%s mins', $mins);
} else if (($diff <= 86400) && ($diff > 3600)) {
$hours = round($diff / 3600);
if ($hours <= 1)
$since = '1 hour';
else
$since = sprintf('%s hours', $hours );
} elseif ($diff >= 86400) {
$days = round($diff / 86400);
if ($days <= 1)
$since = '1 day';
else
$since = sprintf('%s days', $days );
}
return $since;
}



for ($i = 0; $i <= 4; $i++) {
echo $tweet[$i]->text;
echo "<br />";
echo $tweet[$i]->created_at;
echo "<br />";


$tempo = $tweet[0]->created_at;
$tempo2 = explode(" ", $tempo, 6);
$some_date = "$tempo2[0] $tempo2[1] $tempo2[2] $tempo2[3] $tempo2[4] $tempo2[5]";
echo "Data for" .($i+1). "th tweet<br />";
echo difference_in_time($some_date) ."<br />";
echo time() ."<br />";

$tempoforum = difference_in_time($some_date) ."<br />";
echo human_time_diff( time(), $tempoforum ) ." ago <br /><br />";
}

?>