PHP - format date ISO8601?











up vote
5
down vote

favorite
2












I have a year (2002) and I'm trying to get it into the following format:



2002-00-00T00:00:00



I tried various iterations, the last of which was this:



$testdate = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
echo date_format($testdate, '2002');


But, even if I come close, it always seems to add +00:00 to the end of it...










share|improve this question




























    up vote
    5
    down vote

    favorite
    2












    I have a year (2002) and I'm trying to get it into the following format:



    2002-00-00T00:00:00



    I tried various iterations, the last of which was this:



    $testdate = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
    echo date_format($testdate, '2002');


    But, even if I come close, it always seems to add +00:00 to the end of it...










    share|improve this question


























      up vote
      5
      down vote

      favorite
      2









      up vote
      5
      down vote

      favorite
      2






      2





      I have a year (2002) and I'm trying to get it into the following format:



      2002-00-00T00:00:00



      I tried various iterations, the last of which was this:



      $testdate = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
      echo date_format($testdate, '2002');


      But, even if I come close, it always seems to add +00:00 to the end of it...










      share|improve this question















      I have a year (2002) and I'm trying to get it into the following format:



      2002-00-00T00:00:00



      I tried various iterations, the last of which was this:



      $testdate = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
      echo date_format($testdate, '2002');


      But, even if I come close, it always seems to add +00:00 to the end of it...







      php date iso8601






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 10:52









      wanttobeprofessional

      90731223




      90731223










      asked Sep 9 '11 at 22:07









      KumbaThought

      68239




      68239
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          13
          down vote



          accepted










          The 'c' format in PHP always appends the timezone offset. You can't avoid that. But you can build the date yourself from components:



          date('Y-m-dTH:i:s', $testdate);





          share|improve this answer





















          • I always seem to make things more complicated than necessary... Thanks!
            – KumbaThought
            Sep 9 '11 at 22:16






          • 1




            omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
            – Mike S.
            Jul 19 '12 at 18:18






          • 4




            gmdate('Y-m-dTH:i:sZ', $date)
            – 4esn0k
            Oct 3 '12 at 14:43


















          up vote
          0
          down vote













          date('Y-m-dTH:i:sZ', time() - date('Z'));





          share|improve this answer

















          • 5




            elaborate more for better explanation
            – Rumit Patel
            Feb 27 at 6:39










          • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
            – Toby Speight
            Feb 27 at 12:47


















          up vote
          0
          down vote













          Best way is to use constants (PHP 5 >= 5.5.0, PHP 7)



          date(DATE_ISO8601, $timeToChange);


          Docs:
          http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types






          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',
            autoActivateHeartbeat: false,
            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%2f7367953%2fphp-format-date-iso8601%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            13
            down vote



            accepted










            The 'c' format in PHP always appends the timezone offset. You can't avoid that. But you can build the date yourself from components:



            date('Y-m-dTH:i:s', $testdate);





            share|improve this answer





















            • I always seem to make things more complicated than necessary... Thanks!
              – KumbaThought
              Sep 9 '11 at 22:16






            • 1




              omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
              – Mike S.
              Jul 19 '12 at 18:18






            • 4




              gmdate('Y-m-dTH:i:sZ', $date)
              – 4esn0k
              Oct 3 '12 at 14:43















            up vote
            13
            down vote



            accepted










            The 'c' format in PHP always appends the timezone offset. You can't avoid that. But you can build the date yourself from components:



            date('Y-m-dTH:i:s', $testdate);





            share|improve this answer





















            • I always seem to make things more complicated than necessary... Thanks!
              – KumbaThought
              Sep 9 '11 at 22:16






            • 1




              omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
              – Mike S.
              Jul 19 '12 at 18:18






            • 4




              gmdate('Y-m-dTH:i:sZ', $date)
              – 4esn0k
              Oct 3 '12 at 14:43













            up vote
            13
            down vote



            accepted







            up vote
            13
            down vote



            accepted






            The 'c' format in PHP always appends the timezone offset. You can't avoid that. But you can build the date yourself from components:



            date('Y-m-dTH:i:s', $testdate);





            share|improve this answer












            The 'c' format in PHP always appends the timezone offset. You can't avoid that. But you can build the date yourself from components:



            date('Y-m-dTH:i:s', $testdate);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 9 '11 at 22:12









            Marc B

            312k31317417




            312k31317417












            • I always seem to make things more complicated than necessary... Thanks!
              – KumbaThought
              Sep 9 '11 at 22:16






            • 1




              omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
              – Mike S.
              Jul 19 '12 at 18:18






            • 4




              gmdate('Y-m-dTH:i:sZ', $date)
              – 4esn0k
              Oct 3 '12 at 14:43


















            • I always seem to make things more complicated than necessary... Thanks!
              – KumbaThought
              Sep 9 '11 at 22:16






            • 1




              omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
              – Mike S.
              Jul 19 '12 at 18:18






            • 4




              gmdate('Y-m-dTH:i:sZ', $date)
              – 4esn0k
              Oct 3 '12 at 14:43
















            I always seem to make things more complicated than necessary... Thanks!
            – KumbaThought
            Sep 9 '11 at 22:16




            I always seem to make things more complicated than necessary... Thanks!
            – KumbaThought
            Sep 9 '11 at 22:16




            1




            1




            omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
            – Mike S.
            Jul 19 '12 at 18:18




            omg! I was looking for this for a long time. I figured out the 'c' part, but didn't know that I had to escape the T. I kept getting timezone for T. Whew! Thanks.
            – Mike S.
            Jul 19 '12 at 18:18




            4




            4




            gmdate('Y-m-dTH:i:sZ', $date)
            – 4esn0k
            Oct 3 '12 at 14:43




            gmdate('Y-m-dTH:i:sZ', $date)
            – 4esn0k
            Oct 3 '12 at 14:43












            up vote
            0
            down vote













            date('Y-m-dTH:i:sZ', time() - date('Z'));





            share|improve this answer

















            • 5




              elaborate more for better explanation
              – Rumit Patel
              Feb 27 at 6:39










            • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
              – Toby Speight
              Feb 27 at 12:47















            up vote
            0
            down vote













            date('Y-m-dTH:i:sZ', time() - date('Z'));





            share|improve this answer

















            • 5




              elaborate more for better explanation
              – Rumit Patel
              Feb 27 at 6:39










            • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
              – Toby Speight
              Feb 27 at 12:47













            up vote
            0
            down vote










            up vote
            0
            down vote









            date('Y-m-dTH:i:sZ', time() - date('Z'));





            share|improve this answer












            date('Y-m-dTH:i:sZ', time() - date('Z'));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 27 at 6:24









            Frank

            11




            11








            • 5




              elaborate more for better explanation
              – Rumit Patel
              Feb 27 at 6:39










            • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
              – Toby Speight
              Feb 27 at 12:47














            • 5




              elaborate more for better explanation
              – Rumit Patel
              Feb 27 at 6:39










            • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
              – Toby Speight
              Feb 27 at 12:47








            5




            5




            elaborate more for better explanation
            – Rumit Patel
            Feb 27 at 6:39




            elaborate more for better explanation
            – Rumit Patel
            Feb 27 at 6:39












            Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
            – Toby Speight
            Feb 27 at 12:47




            Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
            – Toby Speight
            Feb 27 at 12:47










            up vote
            0
            down vote













            Best way is to use constants (PHP 5 >= 5.5.0, PHP 7)



            date(DATE_ISO8601, $timeToChange);


            Docs:
            http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types






            share|improve this answer

























              up vote
              0
              down vote













              Best way is to use constants (PHP 5 >= 5.5.0, PHP 7)



              date(DATE_ISO8601, $timeToChange);


              Docs:
              http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Best way is to use constants (PHP 5 >= 5.5.0, PHP 7)



                date(DATE_ISO8601, $timeToChange);


                Docs:
                http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types






                share|improve this answer












                Best way is to use constants (PHP 5 >= 5.5.0, PHP 7)



                date(DATE_ISO8601, $timeToChange);


                Docs:
                http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 10:22









                fearis

                18613




                18613






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7367953%2fphp-format-date-iso8601%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

                    Bressuire

                    Vorschmack

                    Quarantine