datetimeConversion.php 633 B

12345678910111213141516
  1. <?php
  2. use DateTime;
  3. function datetimeTimestampToString($timestamp) {
  4. $datetimeObject = new DateTime("@$timestamp"); // will supposedly have the timezone set to UTC.
  5. // The spec requires exactly 'GMT' without numeric offset, so we cannot use DateTime::RFC1123.
  6. $datetimeString = $datetimeObject->format("D, d M Y H:i:s") . " GMT";
  7. return $datetimeString;
  8. }
  9. function datetimeStringToTimestamp($datetimeString) {
  10. // Be tolerant and allow any RFC1123 formatted datetime string.
  11. $datetimeObject = DateTime::createFromFormat(DateTime::RFC1123, $datetimeString);
  12. $timestamp = $datetimeObject->getTimestamp();
  13. return $timestamp;
  14. }