Site Admin
Site Admin Founder of MeaningArticles
3292 Views

PHP Convert String to Date/DateTime Example

Hello Dev.

The php offers some useful built-in functions to convert string to Date and DateTime. In this example, I’ll show you how to use strtotime(), and getDate() functions to achieve the desired results:


String to Date and DateTime Conversion using Strtotime() Example:

The strtotime() Syntax

The strtotime() in php function takes Time/Date and now (optional) parameter and returns the numerical value in seconds since Jan 1, 1970.

strtotime(argument);

The getDate() Syntax

The getDate() is useful for retrieving date/time information based on the date and time values passed into the function:

getDate(argument)


Example

<?php 
    $time_var = strtotime("2017/02/30");
    $date_var = getDate($time_var);
    print_r ($date_var);
?> 

/* Result:
    Array(
        [seconds] => 0
        [minutes] => 0
        [hours] => 0
        [mday] => 2
        [wday] => 4
        [mon] => 3
        [year] => 2017
        [yday] => 60
        [weekday] => Thursday
        [month] => March
        [0] => 1488412800
    )
*/

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.