<?php | |
/* | |
CREDITS & LICENCE | |
------------------------------------------------------------------------------------------------------------------------------ | |
Made by Julien Dubedout / judbd.com | |
Released under Do What the Fuck You Want to Public License. https://en.wikipedia.org/wiki/WTFPL | |
PURPOSE | |
------------------------------------------------------------------------------------------------------------------------------ | |
This script is for simple date interval calculation in year/month/days in kirby text for my portfolio | |
eg display "I'm 30 years old and I have 10 years of experience" without update it each year | |
in kirbytext : "I'm (from:1982-06-28) years old and I have (from:2005) years of experience" | |
It does not calculate intervals in hours, minutes or seconds | |
if you want something more complete, try https://github.com/distantnative/relative-date | |
WARNING : THOUGHTS ON DATE FORMATS | |
------------------------------------------------------------------------------------------------------------------------------ | |
Sometimes strtotime does weird shit, and if you can write your date nearly as you want, sometimes it misinterprets | |
dates end leads to false calculations. | |
The bulletproof method is to write it in the the good ol' 'murican way, eg yyyy-mm-dd. | |
you can also try those syntaxes : | |
Forward slash (/) signifies American M/D/Y | |
a dash (-) signifies European D-M-Y | |
a period (.) signifies ISO Y.M.D | |
ACCEPTED CUSTOM FORMATS | |
------------------------------------------------------------------------------------------------------------------------------ | |
http://php.net/manual/en/dateinterval.format.php | |
%Y Years, numeric, at least 2 digits with leading 0 01, 03 | |
%y Years, numeric 1, 3 | |
%M Months, numeric, at least 2 digits with leading 0 01, 03, 12 | |
%m Months, numeric 1, 3, 12 | |
%D Days, numeric, at least 2 digits with leading 0 01, 03, 31 | |
%d Days, numeric 1, 3, 31 | |
%a Total number of days as a result of a DateTime::diff() or (unknown) otherwise 4, 18, 8123 | |
%H Hours, numeric, at least 2 digits with leading 0 01, 03, 23 | |
%h Hours, numeric 1, 3, 23 | |
%I Minutes, numeric, at least 2 digits with leading 0 01, 03, 59 | |
%i Minutes, numeric 1, 3, 59 | |
%S Seconds, numeric, at least 2 digits with leading 0 01, 03, 57 | |
%s Seconds, numeric 1, 3, 57 | |
%R Sign "-" when negative, "+" when positive -, + | |
%r Sign "-" when negative, empty when positive -, | |
You can do custom stuff like (from:2005-03-06 format:It was %y years and %m months ago) | |
It still a little buggy with parenthesis in the (format) espression | |
------------------------------------------------------------------------------------------------------------------------------ | |
*/ | |
kirbytext::$tags['from'] = array( | |
'attr' => array( | |
'to', | |
'format' | |
), | |
'html' => function( $tag ) { | |
# define the default date to now | |
$now = date('Y-m-d'); | |
# grab the "from" argument | |
$from = $tag->attr( 'from' ); | |
# grab the "to" argument, set to "now" if not defined | |
$to = $tag->attr( 'to', $now ); | |
# grab the output format or set it to year if not defined | |
$format = $tag->attr( 'format', 'y' ); | |
# find the $to and $from length | |
$fromLen = strlen( $from ); | |
$toLen = strlen( $to ); | |
# if from is just 4 chars yyyy add additionnal characters to avoid bad interpretation (hours instead of years) by strtotime | |
if ( $fromLen == 4 ) $from .= '-01-01'; | |
# same if date is yyyy-mm | |
elseif ( $fromLen == 7 ) $from .= '-01'; | |
# if it's another format, display error message | |
elseif ( $fromLen != 10 ) throw new Exception( 'invalid date format (try yyyy or yyyy-mm or yyyy-mm-dd).' ); | |
# same check for the $to variable | |
if ( $toLen == 4 ) $to .= '-01-01'; | |
elseif ( $toLen == 7 ) $to .= '-01'; | |
# if the format is wrong, use the current date | |
elseif ( $toLen != 10 ) $to = 'now'; | |
$dtFrom = new DateTime( $from ); | |
$dtTo = new DateTime( $to ); | |
# if the output format is set on "d" ou "j" for days, replace it by "a" the correct syntax for day count. | |
if ( $format == 'j'||$format == 'd' ) $format = 'a'; | |
# if the output format is set on "m" calculate the number in month additionning years ×12 + number of months. | |
if ( $format == 'm' ) return $dtFrom->diff( $dtTo )->m + ($dtFrom->diff( $dtTo )->y * 12); | |
# if the output format is set on "y" or "a" just display it. | |
elseif ( $format == 'y'||$format == 'a') return $dtFrom->diff( $dtTo )->format( '%'.$format ); | |
# if the output format is a custom combination (including % in kirby), display it as is | |
elseif ( strpos($format, '%') !== false ) return $dtFrom->diff( $dtTo )->format( $format ); | |
# else just display in years | |
else return $dtFrom->diff( $dtTo )->format( 'y' ); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment