PHP gmmktime() Tutorial 🎯

beginner
17 min

PHP gmmktime() Tutorial 🎯

Welcome to our comprehensive guide on using the gmmktime() function in PHP! This function is a powerful tool that helps you work with dates and times, making it essential for developers who want to create dynamic and interactive web applications. Let's dive in!

Understanding gmmktime() πŸ“

The gmmktime() function in PHP generates a Unix timestamp from a specified date and time. It is similar to the mktime() function but uses Greenwich Mean Time (GMT) instead of the local time.

php
int gmmktime ( [ int $hour ] , [ int $minute ] , [ int $second ] , [ int $month ] , [ int $day ] , [ int $year ] )
  • $hour: The hour of the day (0-23)
  • $minute: The minute of the hour (0-59)
  • $second: The second of the minute (0-59)
  • $month: The month of the year (1-12)
  • $day: The day of the month (1-31)
  • $year: The year (4 digits, e.g., 2023)

Example 1: Creating a Timestamp πŸ’‘

Let's create a timestamp for the 1st of January, 2000, at 00:00:00 GMT using gmmktime().

php
<?php $timestamp = gmmktime(0, 0, 0, 1, 1, 2000); echo $timestamp; ?>

Example 2: Converting a Timestamp to a Readable Date πŸ’‘

Now that we have a timestamp, let's convert it back into a readable date.

php
<?php $timestamp = 946684800; // Unix timestamp for 1st January, 2000, 00:00:00 (GMT) $date = date("F j, Y", $timestamp); echo $date; ?>

Quiz πŸ“

Quick Quiz
Question 1 of 1

What does the `gmmktime()` function generate in PHP?

Wrapping Up βœ…

With a solid understanding of gmmktime(), you can now create dynamic date-related functionalities in your PHP projects. As you progress, don't forget to explore other PHP date functions like date(), strtotime(), and more. Happy coding! πŸ’»