PHP strtotime() And mktime(): Convert Date To Timestamp

PHP strtotime() And mktime(): Convert Date To Timestamp

Both strtotime() and mktime() are PHP functions used to convert a date or time into a Unix timestamp, which represents the number of seconds that have elapsed since January 1, 1970.

Here's a brief overview of each function and how they differ:

  • strtotime(): This function parses a string containing a US English date format and produces a Unix timestamp.

Here's the basic syntax:

strtotime(string, now) 
  • string: The string to parse. It can be a date, time, or a string with relative date/time formats (like "next Monday", "+1 day", etc.).
  • now (optional): The timestamp used to calculate relative dates. If not provided, the current time is used.

Example:

echo strtotime("now"); // Outputs: current Unix timestamp echo strtotime("10 September 2023"); // Outputs: Unix timestamp for September 10, 2023 echo strtotime("+1 day"); // Outputs: Unix timestamp for one day from now 
  • mktime(): This function generates a Unix timestamp given the date components like hour, minute, second, month, day, and year.

Here's the basic syntax:

mktime(hour, minute, second, month, day, year) 
  • hour, minute, second: The hour, minute, and second. If not provided, they default to current time.
  • month, day, year: The month, day, and year. If not provided, they default to current date.

Example:

echo mktime(0, 0, 0, 9, 10, 2023); // Outputs: Unix timestamp for September 10, 2023 

In summary, use strtotime() when you have a date or time in string format, and use mktime() when you have a date or time in numerical format. Both functions can handle a wide range of dates, from years in the past to many years in the future.

Examples

  1. Convert date to timestamp in PHP with strtotime() and mktime():

    <?php $dateString = "2023-01-01"; // Using strtotime() $timestamp1 = strtotime($dateString); // Using mktime() list($year, $month, $day) = explode('-', $dateString); $timestamp2 = mktime(0, 0, 0, $month, $day, $year); echo $timestamp1; // Outputs: Unix timestamp for January 1, 2023 echo $timestamp2; // Outputs the same Unix timestamp 
  2. PHP strtotime() and mktime() examples:

    <?php // Using strtotime() $timestamp1 = strtotime("2023-01-01"); // Using mktime() $timestamp2 = mktime(0, 0, 0, 1, 1, 2023); echo $timestamp1; // Outputs: Unix timestamp for January 1, 2023 echo $timestamp2; // Outputs the same Unix timestamp 
  3. Using mktime() to create a timestamp from date components in PHP:

    <?php $timestamp = mktime(12, 30, 0, 3, 15, 2023); echo $timestamp; // Outputs: Unix timestamp for March 15, 2023, at 12:30 PM 
  4. Convert string to timestamp using mktime() in PHP:

    <?php $dateString = "2023-01-01"; list($year, $month, $day) = explode('-', $dateString); $timestamp = mktime(0, 0, 0, $month, $day, $year); echo $timestamp; // Outputs: Unix timestamp for January 1, 2023 
  5. Generating timestamps with mktime() in PHP:

    <?php $timestamp = mktime(12, 0, 0, date('n'), date('j'), date('Y')); echo $timestamp; // Outputs: Unix timestamp for today at 12:00 PM 
  6. Convert date and time to timestamp with PHP mktime() and strtotime():

    <?php $dateTimeString = "2023-01-01 12:30:00"; // Using strtotime() $timestamp1 = strtotime($dateTimeString); // Using mktime() list($date, $time) = explode(' ', $dateTimeString); list($year, $month, $day) = explode('-', $date); list($hour, $minute, $second) = explode(':', $time); $timestamp2 = mktime($hour, $minute, $second, $month, $day, $year); echo $timestamp1; // Outputs: Unix timestamp for January 1, 2023, at 12:30 PM echo $timestamp2; // Outputs the same Unix timestamp 
  7. mktime() function for handling future and past dates in PHP:

    <?php // Current timestamp $currentTimestamp = time(); // Future date $futureTimestamp = mktime(0, 0, 0, date('n'), date('j') + 7, date('Y')); // Past date $pastTimestamp = mktime(0, 0, 0, date('n'), date('j') - 7, date('Y')); echo $currentTimestamp; // Outputs: Current Unix timestamp echo $futureTimestamp; // Outputs: Unix timestamp for the date 7 days from now echo $pastTimestamp; // Outputs: Unix timestamp for the date 7 days ago 
  8. PHP mktime() for date calculations and manipulation:

    <?php // Adding 2 days to the current date $futureDate = date('Y-m-d', mktime(0, 0, 0, date('n'), date('j') + 2, date('Y'))); echo $futureDate; // Outputs: Date 2 days from now in "YYYY-MM-DD" format 
  9. Using strtotime() and mktime() together for advanced date handling in PHP:

    <?php $dateString = "2023-01-01"; $timestamp1 = strtotime($dateString); list($year, $month, $day) = explode('-', $dateString); $timestamp2 = mktime(0, 0, 0, $month, $day, $year); echo $timestamp1; // Outputs: Unix timestamp for January 1, 2023 echo $timestamp2; // Outputs the same Unix timestamp 
  10. Date formatting and conversion with PHP strtotime() and mktime():

    Both functions can be used in conjunction with date() to format and convert timestamps:

    <?php $timestamp = strtotime("2023-01-01"); $formattedDate = date("F j, Y", $timestamp); echo $formattedDate; // Outputs: January 1, 2023 

More Tags

radix userform apache-commons-fileupload system-properties cordova-cli undefined seed text-alignment pyaudio swing

More Programming Guides

Other Guides

More Programming Examples