Need a date from an array DateTimeObject
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
add a comment |
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
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
add a comment |
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
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
php mysql datetime
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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:
add a comment |
Try to use this http://php.net/manual/en/datetime.format.php
foreach ($dates as $date) {
echo $date->format('Y-m-d');
}
2
Always use English as an external reference; I've edited this.
– Funk Forty Niner
Dec 27 '18 at 22:38
add a comment |
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
});
}
});
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%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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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:
add a comment |
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:
add a comment |
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:
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:
answered Dec 27 '18 at 22:34
ricardosalazarfullstack
524
524
add a comment |
add a comment |
Try to use this http://php.net/manual/en/datetime.format.php
foreach ($dates as $date) {
echo $date->format('Y-m-d');
}
2
Always use English as an external reference; I've edited this.
– Funk Forty Niner
Dec 27 '18 at 22:38
add a comment |
Try to use this http://php.net/manual/en/datetime.format.php
foreach ($dates as $date) {
echo $date->format('Y-m-d');
}
2
Always use English as an external reference; I've edited this.
– Funk Forty Niner
Dec 27 '18 at 22:38
add a comment |
Try to use this http://php.net/manual/en/datetime.format.php
foreach ($dates as $date) {
echo $date->format('Y-m-d');
}
Try to use this http://php.net/manual/en/datetime.format.php
foreach ($dates as $date) {
echo $date->format('Y-m-d');
}
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
add a comment |
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
add a comment |
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.
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%2f53951216%2fneed-a-date-from-an-array-datetimeobject%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
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