date-helper.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Date helper class.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Class WPSEO_Date_Helper
  9. *
  10. * Note: Move this class with namespace to the src/helpers directory and add a class_alias for BC.
  11. */
  12. class WPSEO_Date_Helper {
  13. /**
  14. * Formats a given date in UTC TimeZone format.
  15. *
  16. * @param string $date String representing the date / time.
  17. * @param string $format The format that the passed date should be in.
  18. *
  19. * @return string The formatted date.
  20. */
  21. public function format( $date, $format = DATE_W3C ) {
  22. $immutable_date = date_create_immutable_from_format( 'Y-m-d H:i:s', $date, new DateTimeZone( 'UTC' ) );
  23. if ( ! $immutable_date ) {
  24. return $date;
  25. }
  26. return $immutable_date->format( $format );
  27. }
  28. /**
  29. * Formats a given date in UTC TimeZone format and translate it to the set language.
  30. *
  31. * @param string $date String representing the date / time.
  32. * @param string $format The format that the passed date should be in.
  33. *
  34. * @return string The formatted and translated date.
  35. */
  36. public function format_translated( $date, $format = DATE_W3C ) {
  37. return date_i18n( $format, $this->format( $date, 'U' ) );
  38. }
  39. }