Skip to content

Instantly share code, notes, and snippets.

@HackJack-101
Forked from judbd/from.php
Last active November 23, 2015 00:48
Show Gist options
  • Save HackJack-101/50a56f069e55dbfca22b to your computer and use it in GitHub Desktop.
Save HackJack-101/50a56f069e55dbfca22b to your computer and use it in GitHub Desktop.
<?php
/*
l'idée première est d'ajouter un tag en kirbytext, pour avoir des dates dynamiques en markdown,
par ex pour dire "j'ai 30 ans" et ne pas l'updater tous les ans, mettre j'ai (from:1985) par ex
l'explication de comment ajouter des tags est ici, mais ça ça va : http://getkirby.com/docs/advanced/kirbytext
*/
kirbytext::$tags['from'] = array(
'attr' => array(
'to',
'format'
),
'html' => function($tag)
{
$now = date('Y-m-d');
/* la date de départ qui peut être sous n'importe quelle forme genre 2002 ou 2002-01 ou 2002-05-06 */
$from = $tag->attr('from');
/* On utilise ici la fonction strtotime http://php.net/manual/fr/function.strtotime.php qui permet de lire une chaine de caractères et de la convertir en timestamp */
$fromTimestamp = strtotime($from);
/* la date de fin qui peut être sous n'importe quelle forme genre 2015 ou 2008-01 ou 2012-05-06 */
$to = $tag->attr('to', $now);
$toTimestamp = strtotime($to);
/* le format d'affichage mais on verra ça plus tard */
$format = $tag->attr('format', 'y');
/*
ACCEPTED FORMATS (pour le moment que Y)
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 -,
*/
if ($fromTimestamp === false) {
throw new Exception("Mauvais format de date pour from:");
} elseif ($toTimestamp === false) {
throw new Exception("Mauvais format de date pour to:");
} else {
$from = new DateTime();
$from->setTimestamp($fromTimestamp);
$to = new DateTime();
$to->setTimestamp($toTimestamp);
$interval = $from->diff($to);
/* en vrai les formats ne marchent pas vraiment comme ça à part "a" et "Y" mais je verrai ça plus tard */
return $interval->format('%' . $format);
/* Idée de format "basique" : '%y an(s) %m mois %d jour(s) %h heure(s) %m minute(s) et %s seconde(s)' */
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment