Created
September 24, 2016 09:03
-
-
Save andyhausmann/3f6832ecd1138d76a5750ec5d129c211 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SotaStudio\NewsExtended\ViewHelpers; | |
/*************************************************************** | |
* Copyright notice | |
* | |
* (c) 2015 Andy Hausmann <[email protected]>, SOTA Studio | |
* | |
* All rights reserved | |
* | |
* This script is part of the TYPO3 project. The TYPO3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
/** | |
* Formats a string to timestamp. | |
* | |
* @package TYPO3 | |
* @subpackage news_extended | |
*/ | |
class StrftimeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { | |
/** | |
* Render the supplied DateTime object as a formatted date using strftime. | |
* | |
* @param mixed $date either a DateTime object or a string (UNIX-Timestamp) | |
* @param string $format Format String which is taken to format the Date/Time | |
* @return string Formatted date | |
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception | |
*/ | |
public function render($date = NULL, $format = '') { | |
if ($date === NULL) { | |
$date = $this->renderChildren(); | |
if ($date === NULL) { | |
return ''; | |
} | |
} | |
if ($date instanceof \DateTime) { | |
try { | |
return strftime($format, $date->getTimestamp()); | |
} catch (\Exception $exception) { | |
throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('"' . $date . '" was DateTime and could not be converted to UNIX-Timestamp by DateTime.', 200000001); | |
} | |
} | |
return strftime($format, (int)$date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment