-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

Victor
  • 3
  • 1
  • 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). – erik258 Dec 27 '18 at 22:01

3 Answers3

0

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

foreach ($dates as $date) {
 echo $date->format('Y-m-d');
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Maxim
  • 2,233
  • 6
  • 16
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:

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

Nick
  • 138,499
  • 22
  • 57
  • 95