SimplePortal

Customization => Custom Coding => Topic started by: FireDitto on October 19, 2016, 12:25:49 PM

Title: Re: PHP Time block
Post by: FireDitto on October 19, 2016, 12:25:49 PM
I have the following PHP in a block, which shows how long my forum has been running;

Code: [Select]
$time_diff = time() - strtotime("2012-01-06 04:01:43");

$tage = ($time_diff - $time_diff%86400)/86400;
$rest_zeit = $time_diff - $tage*86400;
$stunden = ($rest_zeit - $rest_zeit%3600)/3600;
$rest_zeit = $time_diff - $tage*86400 - $stunden*3600;
$minuten = ($rest_zeit - $rest_zeit%60)/60;
$rest_zeit = $time_diff - $tage*86400 - $stunden*3600 - $minuten*60;
$sekunden = $rest_zeit;

echo "<center>";
echo $tage." Days, ";
echo $stunden." Hours</center>";

And I love it, but I'd really like to make it roll over to years when it hits hte right number, rather than having thousands of days?

If anyone could help, I'd be grateful because I'm lost XD
Title: Re: Re: PHP Time block
Post by: emanuele on October 19, 2016, 02:36:20 PM
I'm not sure I understood exactly what you want, sorry.

You want to show just the number of years instead of the number of days?
Title: Re: Re: PHP Time block
Post by: FireDitto on October 19, 2016, 09:48:21 PM
Sorry, I wasn't very clear I was in a rush to leave for work.

I'd like it to state;

Years, months, days - rather than - days, hours
Title: Re: Re: PHP Time block
Post by: emanuele on October 20, 2016, 12:49:02 PM
Looking around, I'd say the easiest way is:
Code: [Select]
<?php

$datetime1 
= new DateTime('2012-01-06 04:01:43');
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval $datetime1->diff($datetime2);

echo 
'<center>';
echo 
$interval->format('%y Year %m Month %d Day %h Hours %i Minute %s Seconds');
echo 
'</center>';

Example from:
http://php.net/manual/en/datetime.diff.php
formatting from:
http://php.net/manual/en/function.date-diff.php#115065

Then you can adapt the format the way you want, and if you have doubts feel free to ask. :)
Title: Re: Re: PHP Time block
Post by: FireDitto on October 26, 2016, 12:10:31 PM
Thank you! :D
Title: Re: Re: PHP Time block
Post by: emanuele on October 26, 2016, 12:49:32 PM
yw. :)
SimplePortal 2.3.7 © 2008-2021, SimplePortal