1. <?php
  2. /**
  3. * Strips metadata from timestamps for manual execution.
  4. * Supports older PHP versions.
  5. *
  6. * @param string $timestamp The timestamp string.
  7. * @return string The timestamp without metadata, or the original timestamp if invalid.
  8. */
  9. function stripTimestampMetadata(string $timestamp): string
  10. {
  11. // Check if the timestamp is valid.
  12. if (!filter_var($timestamp, FILTER_VALIDATE_INT)) {
  13. return $timestamp; // Return original if invalid
  14. }
  15. // Convert the timestamp to a DateTime object.
  16. $datetime = new DateTime();
  17. $datetime->setTimestamp((int)$timestamp);
  18. // Format the timestamp without metadata (e.g., remove timezone info).
  19. return $datetime->format('Y-m-d H:i:s');
  20. }
  21. //Example Usage (uncomment to test):
  22. /*
  23. $timestamp = '1678886400';
  24. $stripped_timestamp = stripTimestampMetadata($timestamp);
  25. echo "Original timestamp: " . $timestamp . "\n";
  26. echo "Stripped timestamp: " . $stripped_timestamp . "\n";
  27. $timestamp = '2023-03-15 10:30:00';
  28. $stripped_timestamp = stripTimestampMetadata($timestamp);
  29. echo "Original timestamp: " . $timestamp . "\n";
  30. echo "Stripped timestamp: " . $stripped_timestamp . "\n";
  31. $timestamp = 'invalid_timestamp';
  32. $stripped_timestamp = stripTimestampMetadata($timestamp);
  33. echo "Original timestamp: " . $timestamp . "\n";
  34. echo "Stripped timestamp: " . $stripped_timestamp . "\n";
  35. */
  36. ?>

Add your comment