Need a date from an array DateTimeObject












-1














I need formatted date-time from php array, I received it from mysql like this:



Array(
[0] => Array
(
[Holidays] => DateTime Object
(
[date] => 2018-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)

) )


But I need the new array like this:



Array
(
[0] => 2018-12-31
[1] => 2018-12-07
)


Saw a post like this but it doesn't have datetimeobject










share|improve this question






















  • You could always ask mysql for a DATE built from the DATETIME. I prefer keeping data operations in the database whenever my ORM etc makes that possible; after all, it's a database and it's way faster at mass data operations than php will ever be ( ignoring the decrease in network overhead for sending time data you don't want).
    – Dan Farrell
    Dec 27 '18 at 22:01
















-1














I need formatted date-time from php array, I received it from mysql like this:



Array(
[0] => Array
(
[Holidays] => DateTime Object
(
[date] => 2018-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)

) )


But I need the new array like this:



Array
(
[0] => 2018-12-31
[1] => 2018-12-07
)


Saw a post like this but it doesn't have datetimeobject










share|improve this question






















  • You could always ask mysql for a DATE built from the DATETIME. I prefer keeping data operations in the database whenever my ORM etc makes that possible; after all, it's a database and it's way faster at mass data operations than php will ever be ( ignoring the decrease in network overhead for sending time data you don't want).
    – Dan Farrell
    Dec 27 '18 at 22:01














-1












-1








-1







I need formatted date-time from php array, I received it from mysql like this:



Array(
[0] => Array
(
[Holidays] => DateTime Object
(
[date] => 2018-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)

) )


But I need the new array like this:



Array
(
[0] => 2018-12-31
[1] => 2018-12-07
)


Saw a post like this but it doesn't have datetimeobject










share|improve this question













I need formatted date-time from php array, I received it from mysql like this:



Array(
[0] => Array
(
[Holidays] => DateTime Object
(
[date] => 2018-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)

) )


But I need the new array like this:



Array
(
[0] => 2018-12-31
[1] => 2018-12-07
)


Saw a post like this but it doesn't have datetimeobject







php mysql datetime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 '18 at 21:49









Victor

31




31












  • You could always ask mysql for a DATE built from the DATETIME. I prefer keeping data operations in the database whenever my ORM etc makes that possible; after all, it's a database and it's way faster at mass data operations than php will ever be ( ignoring the decrease in network overhead for sending time data you don't want).
    – Dan Farrell
    Dec 27 '18 at 22:01


















  • You could always ask mysql for a DATE built from the DATETIME. I prefer keeping data operations in the database whenever my ORM etc makes that possible; after all, it's a database and it's way faster at mass data operations than php will ever be ( ignoring the decrease in network overhead for sending time data you don't want).
    – Dan Farrell
    Dec 27 '18 at 22:01
















You could always ask mysql for a DATE built from the DATETIME. I prefer keeping data operations in the database whenever my ORM etc makes that possible; after all, it's a database and it's way faster at mass data operations than php will ever be ( ignoring the decrease in network overhead for sending time data you don't want).
– Dan Farrell
Dec 27 '18 at 22:01




You could always ask mysql for a DATE built from the DATETIME. I prefer keeping data operations in the database whenever my ORM etc makes that possible; after all, it's a database and it's way faster at mass data operations than php will ever be ( ignoring the decrease in network overhead for sending time data you don't want).
– Dan Farrell
Dec 27 '18 at 22:01












3 Answers
3






active

oldest

votes


















0














Assuming all the elements in your array are as you show in your question, you can just use array_map to translate the array. For example:



$a = array(array('Holidays' => new DateTime('2018-01-01', new DateTimeZone('Europe/Berlin'))),
array('Holidays' => new DateTime('2018-01-26', new DateTimeZone('Australia/Sydney'))));
print_r($a);
$a = array_map(function ($v) { return $v['Holidays']->format('Y-m-d'); }, $a);
print_r($a);


Output:



Array (
[0] => 2018-01-01
[1] => 2018-01-26
)


Demo on 3v4l.org






share|improve this answer























  • Thank you so much, guess I need to read about array map. Really this helped me as I want.
    – Victor
    Dec 28 '18 at 14:53



















0














Assuming your array is this:



$array=array("2018-01-01 00:00:00.000000","2018-02-01 06:10:59.000000");


You could try formatting your data using Date and strtotime



foreach($array as $date){
echo "Date Format: ".Date('Y-m-d',strtotime($date))."<br>";
echo "Datetime Format: ".Date('Y-m-d H:i:s',strtotime($date))."<br>";
}


I hope it helps C:






share|improve this answer





























    0














    Try to use this http://php.net/manual/en/datetime.format.php



    foreach ($dates as $date) {
    echo $date->format('Y-m-d');
    }





    share|improve this answer



















    • 2




      Always use English as an external reference; I've edited this.
      – Funk Forty Niner
      Dec 27 '18 at 22:38











    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%2f53951216%2fneed-a-date-from-an-array-datetimeobject%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









    0














    Assuming all the elements in your array are as you show in your question, you can just use array_map to translate the array. For example:



    $a = array(array('Holidays' => new DateTime('2018-01-01', new DateTimeZone('Europe/Berlin'))),
    array('Holidays' => new DateTime('2018-01-26', new DateTimeZone('Australia/Sydney'))));
    print_r($a);
    $a = array_map(function ($v) { return $v['Holidays']->format('Y-m-d'); }, $a);
    print_r($a);


    Output:



    Array (
    [0] => 2018-01-01
    [1] => 2018-01-26
    )


    Demo on 3v4l.org






    share|improve this answer























    • Thank you so much, guess I need to read about array map. Really this helped me as I want.
      – Victor
      Dec 28 '18 at 14:53
















    0














    Assuming all the elements in your array are as you show in your question, you can just use array_map to translate the array. For example:



    $a = array(array('Holidays' => new DateTime('2018-01-01', new DateTimeZone('Europe/Berlin'))),
    array('Holidays' => new DateTime('2018-01-26', new DateTimeZone('Australia/Sydney'))));
    print_r($a);
    $a = array_map(function ($v) { return $v['Holidays']->format('Y-m-d'); }, $a);
    print_r($a);


    Output:



    Array (
    [0] => 2018-01-01
    [1] => 2018-01-26
    )


    Demo on 3v4l.org






    share|improve this answer























    • Thank you so much, guess I need to read about array map. Really this helped me as I want.
      – Victor
      Dec 28 '18 at 14:53














    0












    0








    0






    Assuming all the elements in your array are as you show in your question, you can just use array_map to translate the array. For example:



    $a = array(array('Holidays' => new DateTime('2018-01-01', new DateTimeZone('Europe/Berlin'))),
    array('Holidays' => new DateTime('2018-01-26', new DateTimeZone('Australia/Sydney'))));
    print_r($a);
    $a = array_map(function ($v) { return $v['Holidays']->format('Y-m-d'); }, $a);
    print_r($a);


    Output:



    Array (
    [0] => 2018-01-01
    [1] => 2018-01-26
    )


    Demo on 3v4l.org






    share|improve this answer














    Assuming all the elements in your array are as you show in your question, you can just use array_map to translate the array. For example:



    $a = array(array('Holidays' => new DateTime('2018-01-01', new DateTimeZone('Europe/Berlin'))),
    array('Holidays' => new DateTime('2018-01-26', new DateTimeZone('Australia/Sydney'))));
    print_r($a);
    $a = array_map(function ($v) { return $v['Holidays']->format('Y-m-d'); }, $a);
    print_r($a);


    Output:



    Array (
    [0] => 2018-01-01
    [1] => 2018-01-26
    )


    Demo on 3v4l.org







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 27 '18 at 23:32

























    answered Dec 27 '18 at 22:41









    Nick

    24k91635




    24k91635












    • Thank you so much, guess I need to read about array map. Really this helped me as I want.
      – Victor
      Dec 28 '18 at 14:53


















    • Thank you so much, guess I need to read about array map. Really this helped me as I want.
      – Victor
      Dec 28 '18 at 14:53
















    Thank you so much, guess I need to read about array map. Really this helped me as I want.
    – Victor
    Dec 28 '18 at 14:53




    Thank you so much, guess I need to read about array map. Really this helped me as I want.
    – Victor
    Dec 28 '18 at 14:53













    0














    Assuming your array is this:



    $array=array("2018-01-01 00:00:00.000000","2018-02-01 06:10:59.000000");


    You could try formatting your data using Date and strtotime



    foreach($array as $date){
    echo "Date Format: ".Date('Y-m-d',strtotime($date))."<br>";
    echo "Datetime Format: ".Date('Y-m-d H:i:s',strtotime($date))."<br>";
    }


    I hope it helps C:






    share|improve this answer


























      0














      Assuming your array is this:



      $array=array("2018-01-01 00:00:00.000000","2018-02-01 06:10:59.000000");


      You could try formatting your data using Date and strtotime



      foreach($array as $date){
      echo "Date Format: ".Date('Y-m-d',strtotime($date))."<br>";
      echo "Datetime Format: ".Date('Y-m-d H:i:s',strtotime($date))."<br>";
      }


      I hope it helps C:






      share|improve this answer
























        0












        0








        0






        Assuming your array is this:



        $array=array("2018-01-01 00:00:00.000000","2018-02-01 06:10:59.000000");


        You could try formatting your data using Date and strtotime



        foreach($array as $date){
        echo "Date Format: ".Date('Y-m-d',strtotime($date))."<br>";
        echo "Datetime Format: ".Date('Y-m-d H:i:s',strtotime($date))."<br>";
        }


        I hope it helps C:






        share|improve this answer












        Assuming your array is this:



        $array=array("2018-01-01 00:00:00.000000","2018-02-01 06:10:59.000000");


        You could try formatting your data using Date and strtotime



        foreach($array as $date){
        echo "Date Format: ".Date('Y-m-d',strtotime($date))."<br>";
        echo "Datetime Format: ".Date('Y-m-d H:i:s',strtotime($date))."<br>";
        }


        I hope it helps C:







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 27 '18 at 22:34









        ricardosalazarfullstack

        524




        524























            0














            Try to use this http://php.net/manual/en/datetime.format.php



            foreach ($dates as $date) {
            echo $date->format('Y-m-d');
            }





            share|improve this answer



















            • 2




              Always use English as an external reference; I've edited this.
              – Funk Forty Niner
              Dec 27 '18 at 22:38
















            0














            Try to use this http://php.net/manual/en/datetime.format.php



            foreach ($dates as $date) {
            echo $date->format('Y-m-d');
            }





            share|improve this answer



















            • 2




              Always use English as an external reference; I've edited this.
              – Funk Forty Niner
              Dec 27 '18 at 22:38














            0












            0








            0






            Try to use this http://php.net/manual/en/datetime.format.php



            foreach ($dates as $date) {
            echo $date->format('Y-m-d');
            }





            share|improve this answer














            Try to use this http://php.net/manual/en/datetime.format.php



            foreach ($dates as $date) {
            echo $date->format('Y-m-d');
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 27 '18 at 22:38









            Funk Forty Niner

            80.5k1247101




            80.5k1247101










            answered Dec 27 '18 at 21:58









            Maxim Shubin

            1,20110




            1,20110








            • 2




              Always use English as an external reference; I've edited this.
              – Funk Forty Niner
              Dec 27 '18 at 22:38














            • 2




              Always use English as an external reference; I've edited this.
              – Funk Forty Niner
              Dec 27 '18 at 22:38








            2




            2




            Always use English as an external reference; I've edited this.
            – Funk Forty Niner
            Dec 27 '18 at 22:38




            Always use English as an external reference; I've edited this.
            – Funk Forty Niner
            Dec 27 '18 at 22:38


















            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%2f53951216%2fneed-a-date-from-an-array-datetimeobject%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'