Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu

PHP How to get the duration between the two dates

Posted on 30 September, 202030 September, 2020 by admin

We have a simple function which return the duration between the time() and the unix time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
$createdAt = 1601434800;
 
echo '$createdAt = ' . $createdAt . "\r\n";
$date = new DateTime('@' . $createdAt);
$interval = $date->diff(new DateTime('@' . time()));
// var_dump($interval);
 
// echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ".$interval->h." hours ".$interval->i." minutes ".$interval->s." seconds"."ago";
 
 
function getDuration ($interval) {
  // echo "\r\n".$interval->i."\r\n";
  if ($interval->d) {
    if ($interval->h) {
      return 'published '.$interval->d.' days '.$interval->h.' hours ago';
    } else {
      return 'published '.$interval->d.' days ago';
    }
  } else if ($interval->h) {
    if ($interval->i) {
      return 'published '.$interval->h.' hours '.$interval->i.' minutes ago';
    } else {
      return 'published '.$interval->h.' hours ago';
    }
  } else if ($interval->i) {
    if ($interval->i) {
      return 'published '.$interval->i.' minutes '.$interval->s.' seconds ago';
    } else {
      return 'published '.$interval->s.' seconds ago';
    }
  }
}
 
$duration = getDuration($interval);
echo '$duration = '.$duration."\r\n";
?>

That’s it 🙂

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (13)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant