Hướng dẫn php create datetime from string - php tạo datetime từ chuỗi

date_create_from_format

(Php 5> = 5.3.0, Php 7, Php 8)

DateTime :: createdFromFormat - date_create_from_format - phân tích chuỗi thời gian theo một định dạng được chỉ định -- date_create_from_formatParses a time string according to a specified format

Sự mô tả

Phong cách hướng đối tượng

publicStaticDateTime :: createdFromFormat (String ____10, String

DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
1,? DateTimeZone
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
2 =
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
3): static DateTime::createFromFormat(string
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
0
, string
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
1
, ?DateTimeZone
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
2 =
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
3
): DateTime|false

Trả về một đối tượng DateTime mới biểu thị ngày và thời gian được chỉ định bởi chuỗi

DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
4, được định dạng trong
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
5 đã cho.

Giống như datetimeimmutable :: createdFromFormat () nhưng tạo một đối tượng DateTime.DateTimeImmutable::createFromFormat() but creates an DateTime object.

Phong cách thủ tục

Phiên bản thủ tục lấy đối tượng DateTime làm đối số đầu tiên của nó.DateTime object as its first argument.

Trả về giá trị

Trả về một thể hiện DateTime mới hoặc

DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
6 khi thất bại.
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
6
on failure.

Xem thêm

  • Datetimeimmutable :: createFromFormat () - phân tích chuỗi thời gian theo một định dạng được chỉ định

Làm cách nào để định dạng một ngày thành một chuỗi?

new \DateTime('23/05/2013');

Hãy xem mã đơn giản để chuyển đổi ngày thành chuỗi trong java ..

DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character

Ngày ngày = lịch.getInstance (). GetTime () ;.

Dateformat dateformat = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss") ;.2 gold badges24 silver badges34 bronze badges

Chuỗi strdate = dateformat.format (ngày) ;.May 26, 2013 at 9:03

Hướng dẫn php create datetime from string - php tạo datetime từ chuỗi

Tôi có một chuỗi là '23/05/2013 'và tôi muốn tạo một đối tượng thời gian ngày mới từ đây, vì vậy tôi đã làm:aherlambang

Bất kỳ ý tưởng nào tại sao tôi gặp lỗi này mọi lúc:49 gold badges148 silver badges250 bronze badges

2

DDA

$datetime = new DateTime();
$newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');

5,8142 Huy hiệu vàng24 Huy hiệu bạc34 Huy hiệu đồng

echo $newDate->format('Y-m-d');

Khi được hỏi ngày 26 tháng 5 năm 2013 lúc 9:03May 26, 2013 at 9:36

Hướng dẫn php create datetime from string - php tạo datetime từ chuỗi

AherlambangaherlambangFarkie

14.2K49 Huy hiệu vàng148 Huy hiệu bạc250 Huy hiệu Đồng2 gold badges22 silver badges33 bronze badges

2

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    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);

    Bàn luận

    • Time/Date
    • now(optional)

    Chuyển đổi chuỗi cho đến ngày và DateTime sử dụng một số hàm/phương thức như strtotime (), getDate (). Chúng ta sẽ thấy những gì các chức năng này làm.strtotime () - về cơ bản đây là một hàm trả về số giây được truyền kể từ ngày 1 tháng 1 năm 1970, giống như dấu thời gian của máy Linux. Nó trả về số giây được truyền theo tham số được truyền cho hàm.syntax Returns the number of seconds passed since Jan 1, 1970.

    Tham số This function return the date/time information of the passed parameter(date/time);
    Syntax

    getDate(parameter);

    Loại trả lại trả về số giây trôi qua kể từ ngày 1 tháng 1 năm 1970. 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.

    getDate () Hàm này trả về thông tin ngày/giờ của tham số được truyền (ngày/giờ); cú pháp

    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    7

    Tham số Tham số là tùy chọn vì nó mất thời gian địa phương hiện tại làm tham số mặc định. Loại return, nó trả về thông tin của ngày, ngày, năm, tháng, v.v. trong một mảng.

    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    4
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    6
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1____
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    8
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    3

    echo $newDate->format('Y-m-d');
    
    0
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    4
    echo $newDate->format('Y-m-d');
    
    2

    echo $newDate->format('Y-m-d');
    
    3

    Output:

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

    Mã để chuyển đổi chuỗi thành DateTime

    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    7

    echo $newDate->format('Y-m-d');
    
    5
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    echo $newDate->format('Y-m-d');
    
    7
    echo $newDate->format('Y-m-d');
    
    8

    echo $newDate->format('Y-m-d');
    
    9
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    0
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1
    echo $newDate->format('Y-m-d');
    
    5
     strtotime(parameter);
    4

     strtotime(parameter);
    5
     strtotime(parameter);
    6
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1
     strtotime(parameter);
    8
     strtotime(parameter);
    9
    echo $newDate->format('Y-m-d');
    
    9
     strtotime(parameter);
    4

    echo $newDate->format('Y-m-d');
    
    3

    Output:

    10/Jun/2011 07:00:02
    

    Note1 chúng ta có thể sử dụng "D" ở vị trí của "D" để có được ngày đầu ra We can use “D” in the place of “d” for getting the day in the output

    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    7

    echo $newDate->format('Y-m-d');
    
    5
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    getDate(parameter);
    6
    echo $newDate->format('Y-m-d');
    
    8

    echo $newDate->format('Y-m-d');
    
    9
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    0
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1
    echo $newDate->format('Y-m-d');
    
    5
     strtotime(parameter);
    4

     strtotime(parameter);
    5
     strtotime(parameter);
    6
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1
     strtotime(parameter);
    8
     strtotime(parameter);
    9
    echo $newDate->format('Y-m-d');
    
    9
     strtotime(parameter);
    4

    echo $newDate->format('Y-m-d');
    
    3

    Output:

    Tue/May/2011 03:00:02
    

    Note1 chúng ta có thể sử dụng "D" ở vị trí của "D" để có được ngày đầu ra We can use “H” in the place of “h” for getting the time in 24 Hour format in the output

    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    7

    echo $newDate->format('Y-m-d');
    
    5
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    getDate(parameter);
    6
    echo $newDate->format('Y-m-d');
    
    8

    echo $newDate->format('Y-m-d');
    
    9
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    0
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1
    echo $newDate->format('Y-m-d');
    
    5
     strtotime(parameter);
    4

     strtotime(parameter);
    5
     strtotime(parameter);
    6
    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    
    1
     strtotime(parameter);
    8
     strtotime(parameter);
    9
    echo $newDate->format('Y-m-d');
    
    9
     strtotime(parameter);
    4

    echo $newDate->format('Y-m-d');
    
    3

    Output:

    Tue/May/2011 15:00:02
    

    Note1 chúng ta có thể sử dụng "D" ở vị trí của "D" để có được ngày đầu ra

    echo $newDate->format('Y-m-d');
    
    5
    DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
    
    9
    getDate(parameter);
    6
    echo $newDate->format('Y-m-d');
    
    8


    Làm thế nào để chuyển đổi chuỗi thành DateTime trong PHP?

    Mã để chuyển đổi một chuỗi thành DateTime $ date = strtotime ($ input); echo date ('d/m/y h: i: s', $ date);$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

    Strtotime có nghĩa là gì trong PHP?

    Hàm strtotime () phân tích dữ liệu văn bản tiếng Anh vào dấu thời gian unix (số giây kể từ ngày 1 tháng 1 năm 1970 00:00:00 gmt).Lưu ý: Nếu năm được chỉ định ở định dạng hai chữ số, các giá trị trong khoảng 0-69 được ánh xạ tới 2000-2069 và các giá trị trong khoảng 70-100 được ánh xạ tới 1970-2000.parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the 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.

    Làm thế nào để có được thời gian với PHP?

    Bạn có thể chỉ cần sử dụng hàm Php Date () để lấy dữ liệu và thời gian hiện tại ở nhiều định dạng khác nhau, ví dụ: ngày ('d-m-y h: i: s'), ngày ('d/m/y h: i: s') , và như thế.use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

    Làm cách nào để định dạng một ngày thành một chuỗi?

    Hãy xem mã đơn giản để chuyển đổi ngày thành chuỗi trong java ...
    Ngày ngày = lịch.getInstance (). GetTime () ;.
    Dateformat dateformat = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss") ;.
    Chuỗi strdate = dateformat.format (ngày) ;.