PHP : getting Timezone offset from user side and calculate "time ago" - php

I am trying to calculate time passed since a comment is posted. I found a function for this and it's working well
But I just noticed the time shown to user is wrong because of his/her timezone. I did some research and the solution seems to be passing the user's timezone offset to the php page using a javascript function called getTimezoneOffset.
the problem is that I can't manage to use this Offset to make a timezone and use it on that function I linked above. With the help of another code is what I could gather so far :
function humanTiming ($time,$offset)
{
$isDST = 1; // Daylight Saving 1 - on, 0 - off
$timezoneName = timezone_name_from_abbr('', intval($offset, 10) * 36, $isDST);
$date = new DateTime($time, new DateTimeZone($timezoneName));
$time = strtotime($date);
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
And let's call the function like this :
echo humanTiming ($row['date'],"-240");
note : -240 is the value I get from running that javascript function, So it is probably my timezone offset.
First issue: It seems the value -240is invalid and something like -0500 works.
Second issue: even If I try with the valid offset value, the function returns 42 years
Not sure how this 42 years is calculated but its totally wrong.

A couple of problems:
The Javascript function getTimezoneOffset() returns the timezone offset in minutes, however timezone_name_from_abbr() expects the offset in seconds. So in your example of -240 that is actually -4 hours or -14396 seconds. You can fix your code by changing the math a little:
$timezoneName = timezone_name_from_abbr('', intval($offset) * 60, $isDST);
Since you've started using the DateTime object, you can't then use strtotime to get the Unix timestamp. Instead you need format():
$date = new DateTime($time, new DateTimeZone($timezoneName));
$time = $date->format('U');
This should get the result you are after. You were getting 42 years because the time was set to 0 (strtotime($date) evaluated to false) which is Unix epoch - 1970.

You could offset everything like so:
$today=new DateTime("-$offset minutes");
$tomorrow=new DateTime("+1 day-$offset minutes");

Related

Should I store the result of an function into an array?

I have a function like this:
function time_elapsed_string($ptime)
{
$date_time = strtotime("1348-10-10 04:30:01") + $ptime;
$year = date("Y",$date_time);
$month = date("m",$date_time);
$day = date("d",$date_time);
$time = date("H:i:s",$date_time);
$etime = time() - $ptime + 1;
$a = array( 31536000 => 'year',
2592000 => 'month',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
// EX:
return array('date' => $day.'-'.$month.'-'.$year, // 2016-02-20
'time' => $time, // 03:30:04
'difference' => $r . ' ' . $str . ' ago' // 2 month ago
);
}
}
}
And I use it like this:
$ptime = 1470692661;
$html = '<span title="date: '.time_elapsed_string($ptime)['date'].' time: '.time_elapsed_string($ptime)['time'].'">in '.time_elapsed_string($ptime)['difference'].'<span>';
As you see, I'm using of that function's result like this:
time_elapsed_string($ptime)['date']
ime_elapsed_string($ptime)['time']
time_elapsed_string($ptime)['difference']
In fact I'm calling that function every time I need one of its results. Is that right? Or should I call it once and store it into an array?
Note: My code works as well.
Counting time elapsed since some date/time like this is mauvais ton.
DateTime has been available since PHP 5.2.0 and tonns of people underestimate it. Why don't you use this instead of loops and ifs?
$create_time = "2016-08-02 12:35:04";
$current_time="2016-08-02 16:16:02";
$dtCurrent = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
// to use current timestamp, use the following:
//$dtCurrent = new DateTime();
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);
Now, you can format the result however you want:
$interval = $diff->format("%h hours %i minutes %s seconds");
This will give a clean 3 hours 40 minutes 58 seconds without any arrays, which is better.
UPDATE
There is a general solution to get hours / minutes / seconds via regex:
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
// now remove zero values
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
UPDATE 2
As of your comment:
Look, I want to use your approach .. but I really cannot implement it .. Actually I need three things: time, date, difference ..! But your approach doesn't give me them..
Well, we already know how to get the difference, it's the $interval variable described above.
To get time and date, you can get it from the $dtCreate variable by, again, using format:
$time = $dtCreate->format('H:i:s');
$date = $dtCreate->format('d-m-Y');
This is a no brainer.
Yes - store the function call result of time_elapsed_string($ptime) in an array, then use that to access your results. You're wasting CPU cycles otherwise!
// call it once
$result = time_elapsed_string($ptime);
// then use:
$result['date'];
$result['time'];
$result['difference'];

how to use function for array data [duplicate]

This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
i have a span tag which has array data like
<?php echo " <span >".$comments_array[$j]['posted_time']."</span> "; ?>
and it echo's time of the comment as 2014-04-11 05:07:52
now i have a function that display the time in the format of x hrs ago,
below is the function
define("SECOND", 1);
define("MINUTE", 60 * SECOND);
define("HOUR", 60 * MINUTE);
define("DAY", 24 * HOUR);
define("MONTH", 30 * DAY);
function relTime($time)
{
$now = new DateTime;
$dateObj = new DateTime($dt);
$diff = (array) $now->diff($dateObj);
$diff['w'] = floor($diff['d'] / 7);
$diff['d'] -= $diff['w'] * 7;
$tokens = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second'
);
foreach ($tokens as $unit => &$text)
{
if ($diff[$unit])
{
$text = sprintf(
'%s %s%s',
$diff[$unit], $text, ($diff[$unit] > 1 ? 's' : '')
);
}
else
{
unset($tokens[$unit]);
}
}
return array_shift($tokens);
}
now how can i call that function and echo that time in desired format
help me please
There's a better and cleaner way to achieve this. Here's a function, which, given a date/time string and a format string, will return the difference from now in the desired format.
function getDatetimeDifference($datetime, $format) {
$datetime_obj = new DateTime($datetime);
$now = new DateTime();
$difference = date_diff($now, $datetime_obj);
$result = $difference->format($format);
return $result;
}
Here's a use case:
echo getDatetimeDifference(
'2013-04-11 10:35:33',
'%R%a days, %h hours, %i minutes, %s seconds ago'
);
// Outputs "-364 days, 23 hours, 55 minutes, 10 seconds ago"
If you want to work with the total amount of hours since that time, you will fare better if you work with timestamps, since you can easily substract the difference and then divide it by 3600 to get the time difference in hours.
Keep in mind that the DateTime objects will overflow if you don't pass the appropriate format - for example, if you use the above function to get only the hours between now and the last year, you will get 23 as result. My advice - use a similar function, pass a suitable format that you can parse later in tokens, and eliminate left to right tokens until you reach a non-negative value.
Cheers.

PHP - subtracting time return nothing

I found a function form SC to output human readable time. like `
5 hours, 1 hour, 5 years, etc
function human_time ($time)
{
$time = time() - strtotime($time); // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
And I have time as string: 2013-09-28 20:55:42
when I call this function human_time('2013-09-28 20:55:42')
then it return nothing, why ?
I have added strtotime in above function.
Please tell me what is wrong.
This is no ready-to-use code, but rather supposed to guide you the right way:
$then = new DateTime($time); // you might need to format $time using strtotime or other functions depending on the format provided
$now = new DateTime();
$diff = $then->diff($now, true);
echo $diff->format('Your style goes here');
See DateTime Manual for further documentation or feel free to ask here.
Edit: Link fixed.
Use example :
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('2013-05-01 00:22:35', true);
Output :
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Link to the function.

Writing a PHP function that converts UK date to relative time

What would be the best way of converting UK date and time in the format:
30/01/2013 13:30:06
which is d/m/Y H:i:s in PHP noation to relative time (i.e. just now, few minutes ago, 30 minutes ago, 3 hours ago, 1 day ago.. and so on). I've seen several tutorials on the subject but they all revolve around creating functions without any clear explanations. I would appreciate some assistance on the matter.
Hope This helps
function timeSince($ptime){
$etime = time() - strtotime($ptime);
if( $etime < 1 ){
return 'less than 1 second ago';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $a as $secs => $str ){
$d = $etime / $secs;
if( $d >= 1 ){
$r = round( $d );
return ' <font style="color:#0099ff"> ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago</font>';
}
}
}
Writing a custom function might help. Cut the string and convert to numbers. Use mktime() to create a timestamp, compare it to time() current timestamp and switch (case) through various relative time possibilities.
Use mktime(); and date();
For example if you have a specific time in the format d/m/Y H:i:s, and you want it as d-m-Y H:s:i, you would explode the time into chunks using explode(); and then use date(new_format, mktime(current_format_chunks))
dd/mm/yyyy needs to be changed to dd.mm.yyyy according to the formatting rules otherwise it will be treated as mm/dd/yyyy
$dateString = '30/01/2013 13:30:06';
$dateObject = new DateTime(str_replace('/', '.', $dateString));
with the optional addition of a DateTimezone as a second argument to the DateTime constructor.
Then you can do a diff with the current date, and use dateintervals to get the relative time
The most straight-forward and friendly way of parsing dates is the DateTime extension. It has a static method called createFromFormat:
$date = '30/01/2013 13:30:06';
$format = 'j/m/Y G:i:s';
$time = DateTime::createFromFormat($format, $date);
echo $time->format('l dS F \'y at H.i.s');
The method takes a custom format and a date string. Because you can define the format yourself it is much easier than parsing it "manually".
In order to adjust the date you can use the add(), sub() and modify() methods:
$time->add(new DateInterval('P3DT5H')); // 3 days and 5 hours
echo $time->format('l dS F \'y at H.i.s');
$time->sub(new DateInterval('P9DT1H')); // 9 days and 1 hours
echo $time->format('l dS F \'y at H.i.s');
$time->modify('-1 year -35 days');
echo $time->format('l dS F \'y at H.i.s');
As you can see the modify() method is slightly easier to use. The two other methods use the DateInterval class and an awkward format. It is not difficult (just read the documentation and do as it says), but using actual words (i.e. "-3 days -7 hours") is easier to understand.

How to return the amount of years passed?

My friend and I are working on a fairly basic uptime script for an IRC Bot.
Here's our code:
function Uptime()
{
global $uptimeStart;
$currentTime = time();
$uptime = $currentTime - $uptimeStart;
$this->sendIRC("PRIVMSG {$this->ircChannel} :Uptime: ".date("z",$uptime)." Day(s) - ".date("H:i:s",$uptime));
}
$uptimeStart is set immediately when the script runs, as time();
for some reason when I execute this function, it starts at 364 days and 19 hours. I can't figure out why.
Your $uptime is not a timestamp as should be used in date(), but a difference in time. You have an amount of seconds there, not a timestamp (that corresponds with an actual date.
just use something like this to cacluate (quick one, put some extra brain in for things like 1 day, 2 hours etc) ;)
$minutes = $uptime / 60;
$hours = $minuts/60 ;
$days = $hours / 24
etc
If you have 5.3 or above, use the DateTime and DateInterval classes:
$uptimeStart = new DateTime(); //at the beginning of your script
function Uptime() {
global $uptimeStart;
$end = new DateTime();
$diff = $uptimeStart->diff($end);
return $diff->format("%a days %H:%i:%s");
}
You won't get anything meaninful by calling date() on that time difference. You should take that time difference and progressively divide with years, months, days, hours, all measured in seconds. That way you'll get what the time difference in those terms.
$daySeconds = 86400 ;
$monthSeconds = 86400 * 30 ;
$yearSeconds = 86400 * 365 ;
$years = $uptime / $yearSeconds ;
$yearsRemaining = $uptime % $yearSeconds ;
$months = $yearsRemaining / $monthSeconds ;
$monthsRemaining = $yearsRemaining % $monthSeconds ;
$days = $monthsRemaining / $daySeconds ;
.. etc to get hours and minutes.
date() function with second argument set to 0 will actually return you (zero-date + (your time zone)), where "zero-date" is "00:00:00 1970-01-01". Looks like your timezone is UTC-5, so you get (365 days 24 hours) - (5 hours) = (364 days 19 hours)
Also, date() function is not the best way to show the difference between two dates. See other answers - there are are already posted good ways to calculate difference between years

Categories