LiveTime.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ TnxuW6ilFsbVg6dzl5sRjMznCRLVCk1V4l0vG54/B/s=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * live date & time view helper class
  14. */
  15. namespace Ppb\View\Helper;
  16. use Cube\View\Helper\AbstractHelper;
  17. class LiveTime extends AbstractHelper
  18. {
  19. const ELEMENT_ID = 'live-time-id';
  20. const DATE_FORMAT = 'F d, Y H:i:s';
  21. /**
  22. *
  23. * the format the date will be output in (strftime format required)
  24. *
  25. * @var string
  26. */
  27. protected $_format;
  28. /**
  29. *
  30. * class constructor
  31. *
  32. * @param string $format the format the date will be output in (strftime format required)
  33. */
  34. public function __construct($format)
  35. {
  36. $this->_format = $format;
  37. }
  38. /**
  39. *
  40. * display a formatted date (w/ live clock component)
  41. *
  42. * @param string $date
  43. * @param null $format
  44. * @return string
  45. */
  46. public function liveTime($date, $format = null)
  47. {
  48. if ($date === null) {
  49. return 'n/a';
  50. }
  51. if (!is_numeric($date)) {
  52. $date = strtotime($date);
  53. }
  54. if ($format === null) {
  55. $format = $this->_format;
  56. }
  57. $this->_generateJavascript(
  58. date(self::DATE_FORMAT, $date));
  59. $format = str_replace(
  60. array(':', '%H', '%I', '%l', '%M', '%p', '%P', '%r', '%R', '%S', '%T', '%X', '%z', '%Z'), '', $format);
  61. return strftime($format, $date)
  62. . ' '
  63. . '<span id="' . self::ELEMENT_ID . '"></span>';
  64. }
  65. protected function _generateJavascript($dateTime)
  66. {
  67. /** @var \Cube\View\Helper\Script $scriptHelper */
  68. $scriptHelper = $this->getView()->getHelper('script');
  69. $scriptHelper->addBodyCode("<script type=\"text/javascript\">" . "\n"
  70. . " var serverDate = new Date('{$dateTime}'); " . "\n"
  71. . " function padLength(value){ " . "\n"
  72. . " var output=(value.toString().length==1)? '0' + value : value; " . "\n"
  73. . " return output; " . "\n"
  74. . " } " . "\n"
  75. . " function displayTime() { " . "\n"
  76. . " serverDate.setSeconds(serverDate.getSeconds() + 1) " . "\n"
  77. . " var timeString=padLength(serverDate.getHours()) + ':' + padLength(serverDate.getMinutes()) + ':' + padLength(serverDate.getSeconds()); " . "\n"
  78. . " document.getElementById('" . self::ELEMENT_ID . "').innerHTML = timeString; " . "\n"
  79. . " } " . "\n"
  80. . " window.onload=function(){ " . "\n"
  81. . " setInterval('displayTime()', 1000) " . "\n"
  82. . " }" . "\n"
  83. . "</script>");
  84. }
  85. }