PHP and MySQL (56 Blogs) Become a Certified Professional

strtotime In PHP: Everything You Need To Know

Published on Jul 18,2019 2.3K Views


PHP which has a date functions to handle date & time to implement in web applications. We can convert human-readable dates to Unix Timestamp. This function is strtotime. In this article we will be exploring strtotime in PHP. Following pointers will be covered in this article,

Let us get started with this srttotime in PHP article,

strtotime In PHP

What is strtotime()?

 It is a built-in function that is used in PHP to convert an English textual date-time to a UNIX timestamp. When we look at dates in PHP, there is a system called UNIX timestamp. If we want to echo out function time, we will be left with a weird number that has absolutely nothing to do with the date.

<?php
echo time()
?>

Output: 1562486153

Let us get started with the next bit srttotime in PHP article,

What is UNIX timestamp?

It is UNIX timestamp in above output and that is nothing more than a linear number that has counted the number of seconds since January 1st of 1970 but it is a useful number because we can format this being nothing more than seconds. We can use PHP to manipulate that string into what we want it to be.If i use the date function, date function requires two parameters, first is the format of date  and second is time source.

<?php
echo date('m-d-y',time());
?>

Output: 07-07-19

This brings to the final bit of this article

Use of strtotime() in PHP

When we are going to echo strtotime(), it will show the UNIX timestamp of that. Lets say year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

<?php
echo strtotime('July 7th 2019');
?>

Output: 1562472000

<?php
echo strtotime('July 7th 2019 2:10 pm');
?>

Output: 1562523000

Essentially it is just turning a string of text into an actual timestamp. This shows Year,month, day on the basis of UTC but not on my time zone

<?php
echo date('Y-m-D',time());
?>

Output: 2019-07-Sun

We can also set the timezone to nearest server by using date_default_timezone_set(). For getting the exact month,date,year and time we use date(‘M-dS-Y at h:i A’,time())

<?php
date_default_timezone_set("Asia/Calcutta");
echo date('M-dS-Y at h:i A',time());
?>

Output:

Jul-07th-2019 at 03:22 PM

With this we come to an end of this article on srttotime in PHP, I hope you have learned about the strtotime(), UNIX timestamp, Use of strtotime() in PHP with examples.

If you found this article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  article and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

strtotime In PHP: Everything You Need To Know

edureka.co