Core PHP

Get the Date and Time for Time Zone

The current time and date for a particular time zone can found by setting the time zone and then calling the getdate() function to get a date object.

The program below accesses various members of the associative date array to get all of the aspects of the current date and time. The fields are as follows:

  • 0 => seconds since January 1, 1970 (midnight UTC/GMT), the Unix Epoch
  • 'mday' => day of the month (1 - 31)
  • 'wday' => day of the week (0 = Sunday, ..., 6 = Saturday)
  • 'mon' => month of the year (1, ..., 12)
  • 'yday' => day of the year (0, ..., 365)
  • 'seconds' => seconds (0, ..., 59)
  • 'minutes' => minutes (0, ..., 59)
  • 'hours' => hours (0, ..., 23)
  • 'weekday' => weekday (Sunday, ..., Saturday)
  • 'month' => month (January, ..., December)
  • 'year' => year (e.g. 2041)

GetDate.php

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
<?php
  date_default_timezone_set('America/Chicago');
  $qaDate = getdate();
  echo "<h3>Default Time Zone: ".date_default_timezone_get()."</h3>".PHP_EOL;
  echo "<ul>".PHP_EOL;
  echo "  <li> &dollar;qaDate[0] = ".$qaDate[0]."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;seconds&apos;] = ".$qaDate['seconds']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;minutes&apos;] = ".$qaDate['minutes']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;hours&apos;] = ".$qaDate['hours']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;weekday&apos;] = ".$qaDate['weekday']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;wday&apos;] = ".$qaDate['wday']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;mday&apos;] = ".$qaDate['mday']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;yday&apos;] = ".$qaDate['yday']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;month&apos;] = ".$qaDate['month']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;mon&apos;] = ".$qaDate['mon']."</li>".PHP_EOL;
  echo "  <li> &dollar;qaDate[&apos;year&apos;] = ".$qaDate['year']."</li>".PHP_EOL;
  echo "</ul>".PHP_EOL;
?>
  </body>
</html>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.