← Back to Chapters

PHP mktime() & gmmktime()

⏳ PHP mktime() & gmmktime()

? Quick Overview

The mktime() and gmmktime() functions in PHP are used to generate Unix timestamps from specific date and time values. The main difference is that mktime() works with the local timezone, while gmmktime() always uses GMT.

? Key Concepts

  • Unix timestamp represents seconds since 1 Jan 1970 (GMT).
  • mktime() is timezone-dependent.
  • gmmktime() ignores local timezone settings.

⚙️ Syntax / Theory

? View Code Example
// Syntax for local time timestamp
mktime(hour, minute, second, month, day, year);

// Syntax for GMT based timestamp
gmmktime(hour, minute, second, month, day, year);

? Code Example: mktime()

? View Code Example
// Generate timestamp using local timezone
<?php
echo mktime(12, 0, 0, 12, 31, 2025);
?>

? Explanation

This creates a Unix timestamp for 31 December 2025, 12:00 PM based on the server’s local timezone.

? Code Example: gmmktime()

? View Code Example
// Generate timestamp using GMT timezone
<?php
echo gmmktime(12, 0, 0, 12, 31, 2025);
?>

? Explanation

This generates a Unix timestamp for the same date and time but strictly using GMT.

? Interactive Timeline (Concept)

Unix Timestamp

? Use Cases

  • Calculating future or past dates programmatically.
  • Timezone-aware logging systems.
  • Scheduling tasks and reminders.
  • Comparing date intervals.

✅ Tips & Best Practices

  • Use gmmktime() for timezone-independent calculations.
  • Combine timestamps with date() for formatting.
  • Always validate date inputs before processing.

? Try It Yourself

  • Generate a timestamp for your birthday using both functions.
  • Compare current time with one month later.
  • Convert a string date into timestamp and format it.