Java, J2EE & SOA Certification Training
- 37k Enrolled Learners
- Live Class
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,
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() ?>
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()); ?>
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'); ?>
<?php echo strtotime('July 7th 2019 2:10 pm'); ?>
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()); ?>
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()); ?>
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.