note 113091 added to class.dateinterval

From: Date: Wed, 28 Aug 2013 19:06:56 +0000
Subject: note 113091 added to class.dateinterval
Groups: php.notes 
Request: Send a blank email to php-notes+get-196106@lists.php.net to get a copy of this message
This DateInterval extension allows you to write a formatted timestamp but omit the "zero values" and handle things like listing, plurals, etc. Example input: '%y year(s)', '%m month(s)', '%d day(s)', '%h hour(s)', '%i minute(s)', '%s second(s)' Example output: 1 year, 2 months, 16 days, 1 minute, and 15 seconds Example input: '%y año(s)', '%m mes(es)', '%d día(s)', '%h hora(s)', '%i minuto(s)', '%s segundo(s)' Example output: 1 año, 2 meses, 16 días, 1 minuto, y 15 segundos <meta charset="UTF-8"> <?php error_reporting(E_ALL); class MyDateInterval extends DateInterval { public $pluralCheck = '()', // Must be exactly 2 characters long // The first character is the opening brace, the second the closing brace // Text between these braces will be used if > 1, or replaced with $this->singularReplacement if = 1 $singularReplacement = '', // Replaces $this->pluralCheck if = 1 // hour(s) -> hour $separator = ', ', // Delimiter between units // 3 hours, 2 minutes $finalSeparator = ', and ', // Delimeter between next-to-last unit and last unit // 3 hours, 2 minutes, and 1 second $finalSeparator2 = ' and '; // Delimeter between units if there are only 2 units // 3 hours and 2 minutes public static function createFromDateInterval (DateInterval $interval) { $obj = new self('PT0S'); foreach ($interval as $property => $value) { $obj->$property = $value; } return $obj; } public function formatWithoutZeroes () { // Each argument may have only one % parameter // Result does not handle %R or %r -- but you can retrieve that information using $this->format('%R') and using your own logic $parts = array (); foreach (func_get_args() as $arg) { $pre = mb_substr($arg, 0, mb_strpos($arg, '%')); $param = mb_substr($arg, mb_strpos($arg, '%'), 2); $post = mb_substr($arg, mb_strpos($arg, $param)+mb_strlen($param)); $num = intval(parent::format($param)); $open = preg_quote($this->pluralCheck[0], '/'); $close = preg_quote($this->pluralCheck[1], '/'); $pattern = "/$open(.*)$close/"; list ($pre, $post) = preg_replace($pattern, $num == 1 ? $this->singularReplacement : '$1', array ($pre, $post)); if ($num != 0) { $parts[] = $pre.$num.$post; } } $output = ''; $l = count($parts); foreach ($parts as $i => $part) { $output .= $part.($i < $l-2 ? $this->separator : ($l == 2 ? $this->finalSeparator2 : ($i == $l-2 ? $this->finalSeparator : ''))); } return $output; } } date_default_timezone_set('America/Phoenix'); $today = new DateTime('today'); echo 'Today is ', $today->format('F d, Y h:ia'), '.<br>', PHP_EOL; // Today is August 28, 2013 12:00am.<br> $expiration = new DateTime('today +1 year +2 months +16 days +1 minute +15 seconds'); echo 'Expires ', $expiration->format('F d, Y h:ia'), '.<br>', PHP_EOL; // Expires November 13, 2014 12:01am.<br> $interval = MyDateInterval::createFromDateInterval($today->diff($expiration)); echo 'That is ', $interval->formatWithoutZeroes('%y year(s)', '%m month(s)', '%d day(s)', '%h hour(s)', '%i minute(s)', '%s second(s)'), ' from now.<br>', PHP_EOL; // That is 1 year, 2 months, 16 days, 1 minute, and 15 seconds from now. $interval->finalSeparator = ', y '; $interval->finalSeparator2 = ' y '; echo 'Que es de ', $interval->formatWithoutZeroes('%y año(s)', '%m mes(es)', '%d día(s)', '%h hora(s)', '%i minuto(s)', '%s segundo(s)'), ' a partir de ahora.'; // Que es de 1 año, 2 meses, 16 días, 1 minuto, y 15 segundos a partir de ahora. // Is that correct? Spanish isn't my strength.... ?> ---- Server IP: 69.147.83.199 Probable Submitter: 68.14.223.28 ---- Manual Page -- http://php.net/manual/en/class.dateinterval.php Edit -- https://master.php.net/note/edit/113091 Del: integrated -- https://master.php.net/note/delete/113091/integrated Del: useless -- https://master.php.net/note/delete/113091/useless Del: bad code -- https://master.php.net/note/delete/113091/bad+code Del: spam -- https://master.php.net/note/delete/113091/spam Del: non-english -- https://master.php.net/note/delete/113091/non-english Del: in docs -- https://master.php.net/note/delete/113091/in+docs Del: other reasons-- https://master.php.net/note/delete/113091 Reject -- https://master.php.net/note/reject/113091 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196106) next »