Framework  3.9
datetime_type_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__)."/abstract_type_renderer.inc");
39 
41 {
42 
44  {
45  }
46 
47  /*
48  * If no template, use formatDateShort, otherwise,
49  * format using template provided the date function:
50  *
51  * http://php.net/manual/en/function.date.php
52  *
53  * default: "m/d/Y g:ia";
54  *
55  * examples:
56  * {my_datetime} returns the default 2/12/2011 12:30am
57  * {my_datetime:l F j, Y g:ia} returns long format Monday February 12, 2011 12:30am
58  * {my_datetime:F d, Y g:ia} returns February 12, 2011 12:30am
59  *
60  * Template shorthand:
61  * {my_timestamp:short} - returns format 1 above
62  * {my_timestamp:daylong} = returns format 2 above
63  * {my_timestamp:long} - returns format 3 above
64  *
65  */
66  static function format($datetime, $template = "")
67  {
68  trace("DateTimeTypeRenderer template is $template and value is $datetime", 5);
69 
70  $text = "";
71 
72  if($datetime && !$template)
73  $template = "m/d/Y g:ia";
74 
75  // Convert shorthand to format needed for date function
76  $template = AbstractTypeRenderer::translationDateTemplate($template);
77 
78  // strip time component if hours is empty
79  if(preg_match("/00:00:00/", $datetime))
80  {
81  $patterns = "/(g|i|a|:|h|s|b|p|t|z|c|r|u)/i";
82  $template = preg_replace($patterns, "", $template);
83  trace("DateTimeTypeRenderer no time component: template is $template and value is $datetime", 5);
84  }
85 
86  trace("DateTimeTypeRenderer template is $template and value is $datetime", 5);
87 
88  if($datetime)
89  {
90  $dateObj = new DateTime($datetime);
91  $text = $dateObj->format($template);
92  }
93  // else if no date return "";
94 
95  return $text;
96  }
97 }
98 
static translationDateTemplate($template, $includeTime=true)
static format($datetime, $template="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010