Posted By

MMDeveloper on 10/02/08


Tagged

php date format


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

AKtivX
dsntos


all available date info


 / Published in: PHP
 

This will return a structured object containing all available date information in the php date() documentation.

$dayInfo = new dayInfo();

can accept 3 types of arguments

  1. Empty - "", will default to today's date $dayInfo = new dayInfo();

  2. date string - "03/17/1982", will attempt to parse it into a date string $dayInfo = new dayInfo("March 17, 1982");

  3. associative array $dateArray = array ( "hour" => 3, "minute" => 0, "second" => 0, "month" => 3, "day" => 17, "year" => 1982 );

$dayInfo = new dayInfo($dateArray);

  1. <?php
  2.  
  3. class dayInfo {
  4. private $date = false;
  5. public $dateInfo = stdClass;
  6.  
  7. public function __construct($dateArray = "") {
  8. $this->dayInfo($dateArray);
  9. }
  10.  
  11. public function dayInfo($dateArray = "") {
  12. if (is_array($dateArray)) {
  13. $this->date = mktime(
  14. $dateArray["hour"],
  15. $dateArray["minute"],
  16. $dateArray["second"],
  17. $dateArray["month"],
  18. $dateArray["day"],
  19. $dateArray["year"],
  20. $dateArray["isDST"]
  21. );
  22. }
  23. else {
  24. if ($dateArray != "") {
  25. $this->date = strtotime($dateArray);
  26. }
  27. else {
  28. $this->date = time();
  29. }
  30. }
  31.  
  32. $this->getDateInfo();
  33. }
  34.  
  35. private function getDateInfo() {
  36. if ($this->date !== false) {
  37. $return = array (
  38. "day" => array(
  39. "d" => date("d", $this->date),
  40. "D" => date("D", $this->date),
  41. "j" => date("j", $this->date),
  42. "l" => date("l", $this->date),
  43. "N" => date("N", $this->date),
  44. "S" => date("S", $this->date),
  45. "w" => date("w", $this->date),
  46. "z" => date("z", $this->date)
  47. ),
  48. "week" => array (
  49. "W" => date("W", $this->date)
  50. ),
  51. "month" => array (
  52. "F" => date("F", $this->date),
  53. "m" => date("m", $this->date),
  54. "M" => date("M", $this->date),
  55. "n" => date("n", $this->date),
  56. "t" => date("t", $this->date)
  57. ),
  58. "year" => array (
  59. "L" => date("L", $this->date),
  60. "o" => date("o", $this->date),
  61. "y" => date("y", $this->date),
  62. "Y" => date("Y", $this->date)
  63. ),
  64. "time" => array (
  65. "a" => date("a", $this->date),
  66. "A" => date("A", $this->date),
  67. "B" => date("B", $this->date),
  68. "g" => date("g", $this->date),
  69. "G" => date("G", $this->date),
  70. "h" => date("h", $this->date),
  71. "H" => date("H", $this->date),
  72. "i" => date("i", $this->date),
  73. "s" => date("s", $this->date)
  74. ),
  75. "timezone" => array (
  76. "e" => date("e", $this->date),
  77. "I" => date("I", $this->date),
  78. "O" => date("O", $this->date),
  79. "P" => date("P", $this->date),
  80. "T" => date("T", $this->date),
  81. "Z" => date("Z", $this->date)
  82. ),
  83. "full" => array (
  84. "c" => date("c", $this->date),
  85. "r" => date("r", $this->date),
  86. "U" => date("U", $this->date)
  87. )
  88. );
  89.  
  90. $this->dateInfo = $this->bindArrayToObject($return);
  91. }
  92. else {
  93. $this->dateInfo = false;
  94. }
  95. }
  96.  
  97. private function bindArrayToObject($array) {
  98. $return = new stdClass();
  99. foreach ($array as $k => $v) {
  100. if (is_array($v)) {
  101. $return->$k = $this->bindArrayToObject($v);
  102. }
  103. else {
  104. $return->$k = $v;
  105. }
  106. }
  107.  
  108. return $return;
  109. }
  110. }
  111.  
  112. $dayInfo = new dayInfo();
  113. print_r($dayInfo);
  114. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: AKtivX on October 9, 2008

Nice work ... thanks for the useful code! I especially like the recursive object binding method at the end.

You need to login to post a comment.