Hướng dẫn date from string php

giải pháp 1.

Sử dụng strtotime() vào buổi hẹn hò đầu tiên của bạn sau đó date('Y-m-d') để chuyển đổi nó trở lại:

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

echo $newformat;
// 2003-10-16

Lưu ý: Có sự khác biệt giữa việc sử dụng dấu gạch chéo lên / và gạch nối - trong strtotime() chức năng.

giải pháp 2

Quan sát sự khác biệt giữa định dạng m / d / Y và mdY.

PHP xem xét / nghĩa là m / d / Y và - nghĩa là dmY. Tôi sẽ mô tả rõ ràng định dạng đầu vào trong trường hợp này:

$ymd = DateTime::createFromFormat('m-d-Y', '10-16-2003')->format('Y-m-d');

https://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Converting the string to Date and DateTime uses several functions/methods like strtotime(), getDate(). We will see what these functions do.
    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)

    Return Type Returns the number of seconds passed since Jan 1, 1970.

    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.

    Code for converting a string to date

    $time_input = strtotime("2011/05/21"); 

    $date_input = getDate($time_input); 

    print_r($date_input);                

    ?>

    Output:

    Array
    (
        [seconds] => 0
        [minutes] => 0
        [hours] => 0
        [mday] => 21
        [wday] => 6
        [mon] => 5
        [year] => 2011
        [yday] => 140
        [weekday] => Saturday
        [month] => May
        [0] => 1305936000
    )
    

    Code for converting a string to dateTime

    $input = '06/10/2011 19:00:02';

    $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

    $input = '05/10/2011 15:00:02';

    $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

    $input = '05/10/2011 15:00:02';

    $date = strtotime($input);

    echo date('D/M/Y H:i:s', $date);

    ?>

    Output:

    Tue/May/2011 15:00:02
    

    Similarly, the “i” and “s” could also be changed to uppercase to find different outputs, it is possible but not of much use.

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    giải pháp 1.

    Sử dụng strtotime() vào buổi hẹn hò đầu tiên của bạn sau đó date('Y-m-d') để chuyển đổi nó trở lại:

    $time = strtotime('10/16/2003');
    
    $newformat = date('Y-m-d',$time);
    
    echo $newformat;
    // 2003-10-16

    Lưu ý: Có sự khác biệt giữa việc sử dụng dấu gạch chéo lên / và gạch nối - trong strtotime() chức năng.

    giải pháp 2

    Quan sát sự khác biệt giữa định dạng m / d / Y và mdY.

    PHP xem xét / nghĩa là m / d / Y và - nghĩa là dmY. Tôi sẽ mô tả rõ ràng định dạng đầu vào trong trường hợp này:

    $ymd = DateTime::createFromFormat('m-d-Y', '10-16-2003')->format('Y-m-d');

    https://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime