Tutorials Algorithms Analysis of Algorithms Data Structures Languages Interview Corner GATE ISRO CS UGC NET CS CS Subjects Web Technologies Student Jobs
Converting the string to Date and DateTime uses several functions/methods like strtotime(), getDate(). We will see what these functions do.
Đang xem: Format date ymd php
Bạn đang xem: Convert string to date and date
strtotime() – This is basically a function which returns the number of seconds passed since Jan 1, 1970, just like a linux machine timestamp. It returns the number of seconds passed according to the parameter passed to the function. Syntax
strtotime(parameter);Parameter
Time/Date now(optional)
getDate() This function return the date/time information of the passed parameter(date/time);Syntax
getDate(parameter);Parameter The parameter is optional as it takes the current local time as default parameter.
Return Type It returns the information of the date, day, year, month etc in an array.
Xem thêm: Lập Trình Chức Năng Tìm Kiếm Trong Php Với Elasticsearch & Php & Mysql Thực Hành
Code for converting a string to date
$date_input = getDate($time_input); print_r($date_input); ?> Output:
Array( => 0 => 0 => 0 => 21 => 6 => 5 => 2011 => 140 => Saturday => May => 1305936000)Code for converting a string to dateTime
$date = strtotime($input); echo date(“d/M/Y h:i:s”, $date); ?> Output:
10/Jun/2011 07:00:02 Note1 We can use “D” in the place of “d” for getting the day in the output
$date = strtotime($input); echo date(“D/M/Y h:i:s”, $date); ?> Output:
Tue/May/2011 03:00:02 Note 2 We can use “H” in the place of “h” for getting the time in 24 Hour format in the output
$date = strtotime($input); echo date(“D/M/Y H:i:s”, $date); ?> Output:
Tue/May/2011 15:00:02Similarly, the “i” and “s” could also be changed to uppercase to find different outputs, it is possible but not of much use.
My Personal Notes arrow_drop_up Save favorite_border Like first_page Previous PHP | strtotime() Function Next last_page PHP | fopen( ) (Function open file or URL) Recommended ArticlesPage : PHP date() format when inserting into datetime in MySQL02, Jan 19PHP | DateTime setDate() Function10, Oct 19PHP | DateTime modify() Function10, Oct 19PHP | DateTime diff() Function10, Oct 19PHP | DateTime format() Function10, Oct 19PHP | DateTime createFromFormat() Function10, Oct 19PHP | DateTime add() Function10, Oct 19PHP | DateTime sub() Function04, Oct 19How to copy a DateTime object in PHP ?22, Apr 20Date after adding given number of days to the given date17, May 17How to convert UTC date time into local date time using JavaScript ?20, Jun 20How to Restrict User to Enter Date Manually in Date Field using AngularJS ?16, Dec 20How to Convert java.sql.Date to java.util.Date in Java?02, Feb 21Converting a string variable into Boolean, Integer or Float type in Golang01, May 20Converting seconds into days, hours, minutes and seconds28, Feb 18Program for converting hours into minutes and seconds28, Dec 18How to add and subtract days using DateTime in Python?31, Dec 20Converting the Slides of a PPT into Images using Java02, Jan 21Converting ArrayList to HashMap in Java 8 using a Lambda Expression03, Jan 21Converting ArrayList to HashMap using Method Reference in Java 805, Jan 21Converting Images to webp Open Source Format in Linux16, Feb 21HTML | DOM Input Datetime Object11, Feb 19How do you display JavaScript datetime in 12 hour AM/PM format?27, Jun 19PHP | Date and Time05, Dec 17Article Contributed By :
RahulRanjan4 RahulRanjan4 Vote for difficulty Easy Normal Medium Hard Expert Report Issue What’s NewData Structures and Algorithms – Self Paced CourseView DetailsAd-Free Experience – darkedeneurope.com PremiumView DetailsMost popular in PHPMore related articles in PHP
Writing code in comment? Please use ide.darkedeneurope.com, generate link and share the link here.
Load Comments 5th Floor, A-118, Sector-136, Noida, Uttar Pradesh – 201305 feedbackdarkedeneurope.com Company Learn Practice Contribute darkedeneurope.com , Some rights reserved We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !