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);
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.
shown in image time format hint ---today print only time like 9:30 AM or 9:30 PM ;;After yesterday print date like 7Oct or 6 Oct and after one year print 2/12/2015.how can do that in PHP.please help me
Here is the PHP script for exactly what you need..
function time_elapsed_string_in_helper($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' => 'min',
's' => 'sec',
);
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);
if (date('d', strtotime($datetime)) != date('d')) {
return date("d F", strtotime($datetime));
}
else if(date('Y', strtotime($datetime)) != date('Y'))
{
return date("m/d/Y", strtotime($datetime));
}
else
{
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
}
There is no builtin for this, strftime is not supposed to read current time.
The way to implement this is to get current time, compare if the printed value is older than two 12 hours or half a year and choose proper format.
A little modify here
function time_elapsed_string_in_helper($datetime) {
if ((date('d', strtotime($datetime)) != date('d')) && (date('Y', strtotime($datetime)) == date('Y'))) {
return date("d/m", strtotime($datetime));
}
else if(date('Y', strtotime($datetime)) != date('Y'))
{
return date("d/m/Y", strtotime($datetime));
}
else
{
return date("h:i a", strtotime($datetime));
}}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My code is like this :
<?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',
'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';
}
$created_date = '2016-06-28 23:30:00';
echo time_elapsed_string($created_date);
?>
I want to make like datetime in notification facebook
It is like this :
June 26 at 11:45
yesterday at 7:00
12 minutes ago
I want to make like that
Any solution to solve my problem?
I have updated your function. Please add required validation
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);
if(!empty($string['y']) || !empty($string['m']) || !empty($string['w']) )
{
echo showdate($datetime,'w')."<br>";
}
elseif(!empty($string['d']) )
{
if($string['d'] > 1)
{
echo showdate($datetime,'w')."<br>";
}
else
{
echo showdate($datetime,'d')."<br>";
}
}
elseif(!empty($string['h']) )
{
echo implode(', ', $string) . 'h ago';//"<br>";
}
elseif(!empty($string['i']) )
{
echo implode(', ', $string) . 'mints ago';//"<br>";
}
elseif( !empty($string['s']))
{
echo 'just now';//"<br>";
}
// return $string ? implode(', ', $string) . ' ago' : 'just now';
}
function showdate($dt,$type)
{
$mydt = new DateTime($dt);
if($type == 'd')
{
$str = 'Yesterday at '.$mydt->format('H:i:s ');
}
elseif($type == 'h')
{
//$str = 'Yesterday at '.$mydt->format('H:i:s ');
}
else
{
$str = $mydt->format('M d');
$str .= ' at '.$mydt->format('H:i:s ');
}
return $str;
}
$created_date = '2016-06-29 04:50:00';
echo time_elapsed_string($created_date);
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
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.