0

I have issue when converting date to a left weeks, hours and months.

Here my code:

$time_elapsed   = time() - $createDate;
$hours      = round($time_elapsed / 3600);
$days       = round($time_elapsed / 86400 );
$weeks      = round($time_elapsed / 604800);
$months     = round($time_elapsed / 2600640 );

But when i display for example $months, i get: 565

$createDate =1470165198; // Created with time(); 15 minutes ago

Supposed to show 0 no ? since there around 15 minutes difference between them.

Brave Type
  • 165
  • 1
  • 2
  • 7

2 Answers2

0

$create_date must be an integer not string thus

$createDate = 1470165198; // no quotes
sietse85
  • 1,488
  • 1
  • 10
  • 26
0

If you make use of the PHP DateTime class it can be very easily done like this

$start = new DateTime('2015-08-01 00:00:00');  // instead of your time()
$end = new DateTime('2016-09-02 01:01:01');

$interval = $start->diff($end);

echo $interval->format('%y year %m Months %d Days %i Minutes %s Seconds');

RESULT:

+1 year +1 Month +1 Day +1 Minute +1 Second

Now if your required output is different you can play with the formatting as much as you like.

To initialize your a DateTime object using your $createDate = time() timestamp you can do :-

$start = new DateTime();
$start->setTimestamp($createDate);
$end = new DateTime('now');

$interval = $start->diff($end);
echo $interval->format('%y year %m Months %d Days %i Minutes %s Seconds');

The PHP DateTime Manual

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149