SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/ColModifierDate.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. /**
  17. * Converts a MySQL date into something friendlier. Works for DATE, TIME, DATETIME AND [BIG]INT.
  18. *
  19. * The default output format is d/m/Y, but can be changed in the constructor.
  20. * Format strings are anything supported by the PHP function date().
  21. **/
  22. class ColModifierDate extends SortedColModifier
  23. {
  24. private $format;
  25.  
  26. /**
  27.   * @param string $format The format (see PHP's date function, {@link http://php.net/manual/en/function.date.php})
  28.   */
  29. public function __construct($format = 'd/m/Y')
  30. {
  31. $this->format = $format;
  32. }
  33.  
  34. /**
  35.   * Modify a column value
  36.   * This value will be html/csv/etc encoded afterwards.
  37.   *
  38.   * @param string $val The incoming value
  39.   * @param string $field_name The name of the field being modified
  40.   * @return string The modified value
  41.   **/
  42. public function modify($val, $field_name)
  43. {
  44. if ($val == '' or $val == '0000-00-00') return '';
  45.  
  46. // Unix timestamp stored in an INT or BIGINT column
  47. if (preg_match('/^[0-9]+$/', $val)) return date($this->format, $val);
  48.  
  49. // DATE/TIME/DATETIME
  50. return date($this->format, strtotime($val));
  51. }
  52.  
  53. }
  54.  
  55.  
  56.