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 ???










share|improve this question
























  • 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















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 ???










share|improve this question
























  • 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













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 ???










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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






share|improve this answer





















  • 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




















up vote
0
down vote



accepted










The solution that worked well for me is using strtotime as @adam suggested.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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






    share|improve this answer





















    • 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

















    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






    share|improve this answer





















    • 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















    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






    share|improve this answer












    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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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




















    • 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














    up vote
    0
    down vote



    accepted










    The solution that worked well for me is using strtotime as @adam suggested.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      The solution that worked well for me is using strtotime as @adam suggested.






      share|improve this answer























        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.






        share|improve this answer












        The solution that worked well for me is using strtotime as @adam suggested.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 13:41









        Elidrissi simo

        143




        143






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues