PHP creating dates from different string formats
up vote
-1
down vote
favorite
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 (e.g 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 ???
php date
add a comment |
up vote
-1
down vote
favorite
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 (e.g 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 ???
php date
Use strtotime first. Then attempt the conversion.
– adam
Nov 10 at 20:24
You've got tilde's in your code instead of apostrophe's, let's hope that was just a copy/paste error in StackOverflow?
– Stephen Lake
Nov 10 at 20:30
@snh this is my first time I use stackover flow app to ask a question this is why.
– Elidrissi simo
Nov 10 at 20:35
@adam using strtotime worked well thank you.
– Elidrissi simo
Nov 16 at 13:44
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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 (e.g 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 ???
php date
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 (e.g 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 ???
php date
php date
edited Nov 11 at 4:08
Andrew Naguib
1,2231420
1,2231420
asked Nov 10 at 20:23
Elidrissi simo
143
143
Use strtotime first. Then attempt the conversion.
– adam
Nov 10 at 20:24
You've got tilde's in your code instead of apostrophe's, let's hope that was just a copy/paste error in StackOverflow?
– Stephen Lake
Nov 10 at 20:30
@snh this is my first time I use stackover flow app to ask a question this is why.
– Elidrissi simo
Nov 10 at 20:35
@adam using strtotime worked well thank you.
– Elidrissi simo
Nov 16 at 13:44
add a comment |
Use strtotime first. Then attempt the conversion.
– adam
Nov 10 at 20:24
You've got tilde's in your code instead of apostrophe's, let's hope that was just a copy/paste error in StackOverflow?
– Stephen Lake
Nov 10 at 20:30
@snh this is my first time I use stackover flow app to ask a question this is why.
– Elidrissi simo
Nov 10 at 20:35
@adam using strtotime worked well thank you.
– Elidrissi simo
Nov 16 at 13:44
Use strtotime first. Then attempt the conversion.
– adam
Nov 10 at 20:24
Use strtotime first. Then attempt the conversion.
– adam
Nov 10 at 20:24
You've got tilde's in your code instead of apostrophe's, let's hope that was just a copy/paste error in StackOverflow?
– Stephen Lake
Nov 10 at 20:30
You've got tilde's in your code instead of apostrophe's, let's hope that was just a copy/paste error in StackOverflow?
– Stephen Lake
Nov 10 at 20:30
@snh this is my first time I use stackover flow app to ask a question this is why.
– Elidrissi simo
Nov 10 at 20:35
@snh this is my first time I use stackover flow app to ask a question this is why.
– Elidrissi simo
Nov 10 at 20:35
@adam using strtotime worked well thank you.
– Elidrissi simo
Nov 16 at 13:44
@adam using strtotime worked well thank you.
– Elidrissi simo
Nov 16 at 13:44
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
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
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
1
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
add a comment |
up vote
0
down vote
accepted
The solution that worked well for me is using strtotime as @adam suggested.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
1
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
add a comment |
up vote
2
down vote
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
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
1
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
add a comment |
up vote
2
down vote
up vote
2
down vote
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
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
answered Nov 10 at 20:27
Stephen Lake
1,07311123
1,07311123
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
1
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
add a comment |
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
1
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
the problem with Carbon::parse is that it throws an error if the format of the date string is wrong instead of returning false as strtotime do.
– Elidrissi simo
Nov 16 at 13:49
1
1
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
Well ya, if a date is wrong, the date is wrong, carbon is used to parse and mutate actual dates, not strings of incorrect dates. An exact match of your question is documented in the Carbon documentation. But fair enough, if strtotime worked, fantastic.
– Stephen Lake
Nov 16 at 13:55
add a comment |
up vote
0
down vote
accepted
The solution that worked well for me is using strtotime as @adam suggested.
add a comment |
up vote
0
down vote
accepted
The solution that worked well for me is using strtotime as @adam suggested.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The solution that worked well for me is using strtotime as @adam suggested.
The solution that worked well for me is using strtotime as @adam suggested.
answered Nov 16 at 13:41
Elidrissi simo
143
143
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243080%2fphp-creating-dates-from-different-string-formats%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use strtotime first. Then attempt the conversion.
– adam
Nov 10 at 20:24
You've got tilde's in your code instead of apostrophe's, let's hope that was just a copy/paste error in StackOverflow?
– Stephen Lake
Nov 10 at 20:30
@snh this is my first time I use stackover flow app to ask a question this is why.
– Elidrissi simo
Nov 10 at 20:35
@adam using strtotime worked well thank you.
– Elidrissi simo
Nov 16 at 13:44