简体   繁体   中英

PHP creating dates from different string formats

I need to import data from excel, and I can not control the format of the date that the user can use (sometimes it's a string). When I try:

Carbon::createFromFormat(‘m-d-Y’, $string)

If the format of the string is different (eg 2007-07-14) the PHP display an error:

The separate symbol cannot be found.

Is there any solution to validate a date string and create a date for any possible format ???

Carbon is quite smart in parsing dates of different formats, however this will require some testing with different formats.

Use the parse() function:

Carbon::parse('Monday next week'); 
Carbon::parse('2018-06-15 12:34:00', 'UTC');
Carbon::parse('02-31-1999');

Check out the documentation for all available formats and how it works: Carbon API docs

对我来说效果很好的解决方案是使用@adam建议的strtotime。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM