0

I'm trying to get time ago by date("Y-m-d h:i:s"); so the output is 2021-07-03 04:52:12 that's good.

And I'm using this function here to get time ago like 1 minute ago

When i use the this function to get time ago it's works fine, But i need the time ago in this TimeZone Asia\Amman

PHP code to echo current date in Asia\Amman

date_default_timezone_set('Asia/Amman');
$updated_at = date("Y-m-d h:i:s");
echo $updated_at;

Output :

2021-07-03 04:52:12

In time ago:

10 hours ago

But it's should be:

2 seconds ago or 1 seconds ago

The function I use:

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';
}

Thanks!

Amir
  • 13
  • 5
  • When are you calling `time_elapsed_string()`? If you're doing it *before* the call to `date_default_timezone_set()` then that may cause problems. Make sure you're calling `date_default_timezone_set()` first. – kmoser Jul 03 '21 at 14:04
  • Unfortunately, the same result: `10 hours ago` – Amir Jul 03 '21 at 14:07
  • Well, 1 minute ago is 1 minute ago, regardless the timezone :) – Zoli Szabó Jul 03 '21 at 14:08
  • Yes!, But how i can echo that's , Can you explain ? – Amir Jul 03 '21 at 14:09
  • You could try `$now = new DateTime; $now->setTimeZone(new DateTimeZone('Asia\Amman')); $ago = new DateTime($datetime, new DateTimeZone('Asia\Amman'));` – Ameer Jul 03 '21 at 14:19
  • Thanks!, I think it's work fine but the `Asia\Amman` it's not supported I think, and the output echo this error: `Uncaught Exception: DateTimeZone: :__construct(): Unknown or bad timezone (Asia\Amman)` – Amir Jul 03 '21 at 14:40
  • Try it with a forward slash - Asia/Amman https://www.php.net/manual/en/timezones.asia.php – Rob Ruchte Jul 03 '21 at 15:17
  • 2
    Thanks @Ameer !, I just added `$now->sub(new DateInterval("PT4H0M1S"));` to your edit and it's solve my problem, So I will edit my question to add the current code for that, Thanks a lot to all! – Amir Jul 03 '21 at 15:20
  • @AmeerDesign: Don't edit the question, add an answer explaining your solution and put the current code for that in there. You can then later mark the question as answered (and everytime you remember you need to do something similar, you can find your answer here). – hakre Jul 03 '21 at 15:50
  • @hakre: Thanks. I will do it! – Amir Jul 05 '21 at 15:24

1 Answers1

0

Answer is:

@Ameer in the comments has edit this:

$now = new DateTime;
$ago = new DateTime($datetime);

To this:

$now = new DateTime; 
$now->setTimeZone(new DateTimeZone('Asia\Amman')); 
$ago = new DateTime($datetime, new DateTimeZone('Asia\Amman'));

But it's doesn't work because Asia\Amman it's not supported in the list that DateTimeZone support so i found this website show the code of Asia\Amman EEST

So the result changes from 10 hours ago to 5 hours ago that's good news

But it's not solve the problem yet

So now I need -5 hours from 5 hours ago

I found this sub(new DateInterval("PT4H0M1S"))

The PT4H means 4 hours

The 0M means 0 minutes

The 1S means 1 seconds

So the sub means - and add means + to DateTime

Anddd the final code of the function is:

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime; 
    $now->setTimeZone(new DateTimeZone('EST')); 
    $now->sub(new DateInterval("PT4H0M1S"));
    $ago = new DateTime($datetime, new DateTimeZone('EST')); 
    $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';
}

And don't forgot to added date_default_timezone_set() after the date time with your specific TimeZone in my case Asia\Amman

have a nice coding!

Amir
  • 13
  • 5