PHP string to date | How to convert string to date and DateTime

PHP string to date | for converting the string to date and DateTime, there are used several methods/functions like strtotime(), getDate(). we will see how these functions work.
strtotime():- This is also a function that returns the number of seconds passed since Jan 1, 1970, so, like Linux machine timestamp. therefore, it returns the number of seconds passed as per the parameter to the function.
Time/Date
now(optional)
Return Type: This is Returns the number of seconds passed since Jan 1, 1970.
getDate():- The getDate() is a function that returns the date/time information of the passed parameter(date/time).
Here, the parameter is optional as it takes the current local time as the default parameter.
Return Type This is Returns the information of the date, day, year, month, etc in an array.
Question 1: How to convert PHP string to date?
|
1 2 3 4 5 |
<?php $input_time = strtotime("2018/02/22"); $input_date = getDate($input_time); print_r($input_date); ?> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Array ( [seconds] => 0 [minutes] => 0 [hours] => 0 [mday] => 28 [wday] => 0 [mon] => 5 [year] => 2017 [yday] => 147 [weekday] => Sunday [month] => May [0] => 1495929600 ) |
Question 2: How to convert PHP string to DateTime
|
1 2 3 4 5 |
<?php $input = '05/21/2011 15:00:03'; $date = strtotime($input); echo date('d/M/Y h:i:s', $date); ?> |
|
1 |
21/May/2011 03:00:03 |
|
1 2 3 4 5 |
<?php $input = '03/9/2011 15:00:03'; $date = strtotime($input); echo date('D/M/Y h:i:s', $date); ?> |
|
1 |
Wed/Mar/2011 03:00:03 |
|
1 2 3 4 5 |
<?php $input = '02/9/2011 15:00:03'; $date = strtotime($input); echo date('D/M/Y H:i:s', $date); ?> |
|
1 |
Wed/Feb/2011 15:00:03 |
Question 3: How to Converting PHP string to Date and DateTime
Therefore, you will be used strtotime() on your first date then date('Y-m-d') to convert it back:
|
1 2 3 4 5 6 |
<?php $time = strtotime('10/16/2003'); $newformat = date('Y-m-d',$time); echo $newformat; // 2003-10-16 ?> |
Note: You can notice that there is a difference between using forward slash / and hyphen – in the strtotime() function.
Dates in the d-m-y or m/d/y formats are disambiguated by looking at the separator between the various components:
furthermore, if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
whereas if the separator is a slash (/), then the American m/d/y is assumed;
Understand More Date Examples
Note: The given following example is the outputs the dates for the next seven Tuesday:
|
1 2 3 4 5 6 7 8 9 |
<?php $start_date=strtotime("Tuesday"); $end_date=strtotime("+7 weeks", $start_date); while ($start_date < $end_date) { echo date("M d", $start_date) . "<br>"; $start_date = strtotime("+1 week", $start_date); } ?> |
|
1 2 3 4 5 6 7 |
May 05 May 12 May 19 May 26 Jun 02 Jun 09 Jun 16 |
As per the result, you can see that the output of the program is Next Seven Tuesday date comes. such as May 05, May 12, May 19, May 26, Jun 02, Jun 09, Jun 16.
Note: The given below example outputs the number of days until the 30th of September:
|
1 2 3 4 5 |
<?php $dt1=strtotime("September 30"); $dt2=ceil(($dt1-time())/60/60/24); echo "There are " . $dt2 ." days until 30th of September."; ?> |
|
1 |
There are 154 days until 30th of September. |
Therefore, you can see that the output of the program, comes there are 154 days until the 30th of September.
Program Forgetting Your Time Zone
For getting your time zome, therefore If the time you got back from the code is not correct, it’s probably because your server is set up for a different timezone or in another country. furthermore, suppose that if you want the time to be correct according to a specific location, you can set the timezone you want to use. There are the given following example below sets the timezone to “New_York/America”, then outputs the current time in the specified format:
|
1 2 3 4 |
<?php date_default_timezone_set("America/New_York"); echo "Current time is " . date("h:i:sa"); ?> |
|
1 |
Current time is 07:56:45am |
Program To Create a Date With mktime()
The facultative timestamp parameter within the date() operate specifies a timestamp. If omitted, the present date and time are used (as within the above examples).
furthermore, the PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the time specified and the Unix Epoch (January 1 1970 00:00:00 GMT)
The given below program to creates a date and time with the date() function from a number of parameters in the mktime() function:
|
1 2 3 4 |
<?php $dt=mktime(15, 11, 52, 7, 10, 2016); echo "The created date is " . date("Y-m-d h:i:sa", $dt); ?> |
|
1 |
The created date is 2016-07-10 03:11:52pm |