PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The strtotime()
function in PHP is used to convert a date or time in string format into a Unix timestamp, which is the number of seconds since January 1, 1970.
Here's the basic syntax of the strtotime()
function:
strtotime(time, now)
time
: 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.Let's look at a few examples:
Example 1: Converting a date string to a timestamp
$timestamp = strtotime("2023-12-01"); echo $timestamp;
In the above example, strtotime()
converts the date string "2023-12-01" into a Unix timestamp.
Example 2: Using relative date/time formats
$timestamp = strtotime("+1 week"); echo $timestamp;
In the above example, strtotime()
calculates the timestamp for one week from now.
Example 3: Converting a date and time string to a timestamp
$timestamp = strtotime("2023-12-01 15:30:00"); echo $timestamp;
In the above example, strtotime()
converts the date and time string "2023-12-01 15:30:00" into a Unix timestamp.
Example 4: Using relative date/time formats with a specific timestamp
$now = strtotime("2023-12-01"); $timestamp = strtotime("+1 week", $now); echo $timestamp;
In the above example, strtotime()
calculates the timestamp for one week from December 1, 2023.
The strtotime()
function is very powerful and can understand a wide variety of date and time formats. However, it's always a good idea to validate the user input and make sure it's in the correct format before passing it to strtotime()
.
How to use strtotime() to parse date and time in PHP:
strtotime()
is used to parse date and time strings into Unix timestamps. Here's a basic example:
<?php $dateString = "2023-01-01"; $timestamp = strtotime($dateString); echo $timestamp; // Outputs: Unix timestamp for January 1, 2023
Extracting date and time from a string with PHP strtotime():
<?php $dateTimeString = "2023-01-01 12:30:00"; $timestamp = strtotime($dateTimeString); echo $timestamp; // Outputs: Unix timestamp for January 1, 2023, at 12:30 PM
PHP strtotime() format string:
strtotime()
understands various date and time formats. It's flexible and can handle strings like "now", "tomorrow", "next week", etc.
Convert string to timestamp in PHP using strtotime():
<?php $dateTimeString = "2023-01-01 12:30:00"; $timestamp = strtotime($dateTimeString); echo $timestamp; // Outputs: Unix timestamp for January 1, 2023, at 12:30 PM
Common date and time formats for PHP strtotime():
Common formats include "YYYY-MM-DD", "YYYY-MM-DD HH:MM:SS", and various relative expressions like "tomorrow", "next week", etc.
Handling date and time strings with strtotime() in PHP:
<?php $dateTimeString = "tomorrow 3pm"; $timestamp = strtotime($dateTimeString); echo $timestamp; // Outputs: Unix timestamp for tomorrow at 3 PM
PHP strtotime() vs. DateTime class:
strtotime()
is simpler and convenient for basic use cases. For advanced features and better object-oriented design, consider using the DateTime
class.
Parsing custom date formats with PHP strtotime():
<?php $customDateString = "15-Jan-2023"; $timestamp = strtotime($customDateString); echo $timestamp; // Outputs: Unix timestamp for January 15, 2023
Converting human-readable date strings to timestamps in PHP:
<?php $humanReadableDate = "Next Monday"; $timestamp = strtotime($humanReadableDate); echo $timestamp; // Outputs: Unix timestamp for next Monday
Timezone considerations with PHP strtotime():
strtotime()
is timezone-sensitive. Make sure to set the timezone using date_default_timezone_set()
or use the DateTime
class for more control.
PHP strtotime() and relative time expressions:
<?php $relativeTime = "2 days ago"; $timestamp = strtotime($relativeTime); echo $timestamp; // Outputs: Unix timestamp for 2 days ago