← Back to Chapters

PHP Strtotime, Strftime & Gmstrftime

? PHP Strtotime, Strftime & Gmstrftime

?️ Quick Overview

The strtotime(), strftime(), and gmstrftime() functions in PHP are used to manipulate and format date and time values. These functions allow converting date strings into timestamps, formatting date strings, and working with GMT time respectively.

? Key Concepts

  • strtotime() – Converts an English textual datetime description into a Unix timestamp.
  • strftime() – Formats a local time or date based on a given format.
  • gmstrftime() – Formats time as GMT instead of local time.

⌛ Syntax / Theory

  • strtotime(string $datetime) parses human-readable date strings.
  • strftime(string $format, int $timestamp) formats local time.
  • gmstrftime(string $format, int $timestamp) formats GMT time.

⌛ Code Example: strtotime()

? View Code Example
// Convert a human-readable date into a Unix timestamp
<?php
$date = "next Monday";
$timestamp = strtotime($date);
echo date("Y-m-d", $timestamp);
?>

ℹ️ Live Output / Explanation

The string "next Monday" is converted into a Unix timestamp and then formatted into a readable date using date().

?️ Code Example: strftime()

? View Code Example
// Format current local time using a custom pattern
<?php
$date = time();
echo strftime("%A, %B %d, %Y", $date);
?>

? Code Example: gmstrftime()

? View Code Example
// Format current time using GMT timezone
<?php
$date = time();
echo gmstrftime("%A, %B %d, %Y", $date);
?>

? Interactive Example

JavaScript Date Comparison

 

 

? Use Cases

  • Parsing user-entered dates into timestamps.
  • Generating formatted dates for reports and logs.
  • Handling GMT time in international applications.

✅ Tips & Best Practices

  • Use strtotime() for flexible date parsing.
  • Prefer clear and unambiguous date strings.
  • Use gmstrftime() when working with global systems.

? Try It Yourself

  • Calculate the timestamp for 15th August 2025.
  • Display the current date in multiple formats.
  • Compare local time and GMT time outputs.