I need to add +7 hours to a timestamp I'm getting from the DB, but I can't change it to unix because I'm using a function to convert it to "2 hours ago" and stuff like that.
How would I go about doing this? Every question I've seen uses strtotime() which doesn't work with the function I'm using.
Here's the function:
function time_passed($datetime, $full = false) {
$datetimes = $datetime;
$now = new DateTime;
$ago = new DateTime($datetimes);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
So I go through this function with for example 2014-09-11 05:14:12 and it outputs 12 hours ago - Although I want it to output 19 hours ago
Why are you trying to add +7 hours? Looks like you are trying to substitute for time zone difference. Why not just declare your $now variable with the timezone setting and let php do the magic for you? new DateTime("now", new DateTimeZone("America/Los_Angeles")); or whatever your timezone is. You can find a list of timezones at http://php.net/timezones
Related
I have a problem. How can i solve this problem.
My Function
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'mounth',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'sencond',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? '' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just time';
}
$dateok="2022-05-15 09:22:16"; // year-mounth-date hours:minute:second
echo time_elapsed_string($dateok);
I want convert data turkish time but my function calculating wrong .
(I Think is $now = new DateTime; my date format does not match maybe We Have GMT+3 Turkish Time ... I Dont know)
Note : $dateok has a variable value.
EXAMPLE 1 :
Suppose now is (current time ) 2022-05-15 10:47:00
Suppose my date is namely $dateok = 2022-05-15 10:46:00
output // 2 hour ago -> this is false -> must be : 1 minute ago
EXAMPLE 2 :
Suppose now is (current time ) 2022-05-15 10:47:00
Suppose my date is namely $dateok = 2022-05-14 10:47:00
output // 21 hour ago -> this is false -> must be : 1 day ago
SOLUTION : add date_default_timezone_set('') and problem solved.
so hello i am trying to use this most used php script linked here
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
but i cannot get it working, i thought i would fill variable with $datetime with the date i want to differente with
i am inputting date with this format "1614084957" but yet i get error
Error: Uncaught Error: Class 'datetime' not found in
i dont know what wrong its included the function is above the desired echo
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
if ($page->id == 1) {
$kat = $pages->get('/kategorie/');
include('chunks/hry.php');
}
in hry.php i jsut have som foreach and ifs
i have foreach for each a link to game a i wanted to use this function to determine when this game was created,
Add \ before your class name
$now = new \DateTime;
$ago = new \DateTime($datetime);
I found this as an example for a time elapsed string. I have a weird thing going on, when I post it from my local web server it goes to my remote database, It will show the time since on my local webserver correctly, but when you post from my live webserver it comes back as 12 hours ago when it was just posted. (That's when it's updated from my remote server to my remote database that are hosted on the webserver. Find it weird that my local one would give me the correct time while my remote would have a 12 hour lapse. If anyone knows why this is that'd be awesome.
static function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
When I return all the details this is what I get on my local server
Server Time: 2016-09-12 02:17:29
2016-09-12 02:04:48 //Posted
12 minutes ago
And when I look at the same thing on my remote server I get
Server Time: 2016-09-12 12:22:37
2016-09-12 12:22:35 //Posted
11 hours ago
This means the times being posted are correct and it's way off on calculating how long it's been.
Here is how the time shows in the database right now:
2016-05-07 21:18:21
Right now, this is how it does to convert it:
function time_elapsed_string($datetime) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
$string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
when I do this it converts everything. BUT i want it to only show however many seconds, however many minutes, however many hours and if it's 24 hours, then it's 1 day, and 1 day. everything else I want it to show in this format: F j, Y g:ia
/**
* Format a unix timestamp in a 'time ago' format,
* e.g. 15 minutes ago or 7 days ago.
*
* #param integer $time
* #return string
*/
function ago($time)
{
$config = array(
array('second', 'seconds'),
array('minute', 'minutes'),
array('hour', 'hours'),
array('day', 'days'),
array('week', 'weeks'),
array('month', 'months'),
array('year', 'years'),
array('decade', 'decades'),
);
list($periods, $lengths, $now) = array($config, [60, 60, 24, 7, 4.35, 12, 10], time());
$difference = $now - $time;
for ($j = 0; $difference >= $lengths[$j] and $j < count($lengths) - 1; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
$period = $difference == 1 ? $periods[$j][0] : $periods[$j][1];
if ($difference == 0)
{
return 'Just now';
}
return "${difference} ${period} ago";
}
$old_date = "2016-05-07 21:18:21";
$date311 = date('d-m-Y H:i:s', strtotime($old_date));
$date31 = date_create($date311);
//creating a date object
date_default_timezone_set("Asia/Calcutta");
$date411 = date('d-m-Y H:i:s');
$date41 = date_create($date411);
//calculating the difference between dates
//the dates must be provided to the function as date objects that's why we were setting them
//as date objects and not just as strings
//date_diff returns an date object wich can be accessed as seen below
$diff341 = date_diff($date41, $date31);
//accesing days
$days1 = $diff341->d;
//accesing months
$months1 = $diff341->m;
//accesing years
$years1 = $diff341->y;
//accesing hours
$hours1=$diff341->h;
//accesing minutes
$minutes1=$diff341->i;
//accesing seconds
$seconds1=$diff341->s;
echo '<center>';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:600px;font-size:16px">
<b>The difference between '.$date311.' and '.$date411.'
<br />is: ' . $years1 . ' year(s), ' . $months1 . ' month(s), '. $days1 . ' day(s), '.$hours1.' hour(s),
'.$minutes1.' minute(s), '.$seconds1.' second(s) </b>
</div><br />';
echo '</center>';
Result :-
Say I have some posts and they all have the time they were submitted.
Now what I want to do is check how long ago they were posted in minutes. For example post 1 was posted 40 minutes ago and post 2 was posted 479 minutes ago and so on…
And of course I would run that $data array in a loop but for the time being its understandable. And also the time includes a timestamp so instead of just the time it includes the date and time.
php:
$data = ["POST1"=>"3:10PM","POST2"=>"3:40PM","POST3"=>"4:20PM","POST4"=>"5:15PM"]
html:
<div>
<p><?php echo $data[0] ?><p>
</div>
Here is what would help you:
<?php
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
$date = date_parse_from_format('Y-d-m H:i:s', '2014-02-05 5:22:35');
$unixTimestamp = mktime(
$date['hour'], $date['minute'], $date['second'],
$date['month'], $date['day'], $date['year']
);
echo time_elapsed_string(date('Y-m-d H:i:s',$unixTimestamp), false);
and here is a working fiddle for you.