I am getting a Collection which contains
[80] => Array
(
[date] => Carbon\Carbon Object
(
[date] => 2018-04-04 17:27:24.000000
[timezone_type] => 3
[timezone] => UTC
)
I want to get the date from here, when I do foreach it gives a Carbon\Carbon Object('date' like this, but then can not access date.
Does anyone know any solution?
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
foreach($analyticsData as $data)
{
print_r($data['date']->toDateString());die;
}
If you like to convert to string, use
$data['date']->toDateTimeString()
Or you can format custom
$data['date']->format('Y/m/d H:i')
Carbon is a nice wrapper around dates. It has a really great API to get things like date subtraction, testing if a date is in a range, formatting dates, etc.
It sounds like you're looking to format an array of Carbon objects to a date.
You can use array_map to produce this kind of result:
$dates_formatted = array_map(function($entry) {
// transform the Carbon object to something like 'Dec 25, 1975'
return $entry['date']->toFormattedDateString();
}, $dates);
Note: I'm assuming your array is named $dates. If you want a more accurate answer, provide the whole output of your collection along with the name of the variable.
To try out other date formatting options check out their (awesome) documentation: https://carbon.nesbot.com/docs/#api-formatting
Related
Cake\ORM\Entity Object
(
[id] => 1
[lead_id] => 9
[policy_start_date] => Cake\I18n\FrozenDate Object
(
[time] => 2011-07-01T00:00:00+00:00
[timezone] => UTC
[fixedNowTime] =>
)
)
I am working in cakephp 3.x . This is my edit entity array. Here the date format as saved in database is showing 'Y-m-d'. Hence it is displaying date also in y-m-d in datepicker. But I am actually using datepicker format d-m-Y for eg. in this case such as 01/07/2011. I am storing in datebase as Y-m-d but i want to display in edit page as d-m-Y. How can I do it ?. Please help.
You can do it using php.
$d = '2017-07-01'; //your date for formatting. such as policy_start_date
$date = date('d-m-Y',strtotime($d));
echo $date;
Well I have found solution ,
if (!empty($entity->policy_start_date)) {
$entity['policy_start_date'] = $entity->policy_start_date->format('d/m/Y');
}
this replaces the date format.
I have a DateTimeZone object, let us call this $TimeZone.
DateTimeZone Object
(
[timezone_type] => 2
[timezone] => Z
)
I want to get timezone_type and timezone as string values.
I have got timezone using the following line-
$timezone=$TimeZone->getName();
But I am not getting $timezone_type.
there is a thread related to this issue see this:Here
A hack can be, you can convert the object to array then access the index:
$date = new DateTimeZone('Europe/London');
$x=(array) $date;
echo $x['timezone_type'];//3
I am using cakephp 3.2 and when i am retrieving data by find query it is giving date fields in this format
Array
(
[0] => Cake\I18n\FrozenDate Object
(
[date] => 2016-08-01 00:00:00
[timezone_type] => 3
[timezone] => UTC
)
)
and time fields in frozentime
Cake\I18n\FrozenTime Object
(
[date] => 2016-10-11 10:00:00
[timezone_type] => 3
[timezone] => UTC
)
I need a common setting or global solution for complete site. So when i fetch the data by find query from database it should give me date time in simple format without any frozendate object.
like this
Array(
[0] => 2016-08-01
)
Simply call ->format('Y-m-d') on your Cake\I18n\FrozenDate object.
There's no need for Cake\I18n\FrozenDate::setToStringFormat() or $this->Time->format()
You can also use TimeHelper for formating datetime in View
Example
echo $this->Time->format(
$YourDateTimeVariable, #Your datetime variable
'Y-MM-d' #Your custom datetime format
);
CakePHP TimeHelper function details is Here
In boostrap.php add
Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');
still it comes with forzenDate object with same params But when you will print in view then it will print the proper format
echo $var->created; // print: 2016-08-01
Reference for Dates Datetime Format Syntax
You can directly print the date object in any custom date format by using inbuilt i18nFormat function.
$frozenDateObj->i18nFormat('dd-MMM-yyyy');
Use datetime-syntax reference for more customization
my code is :
$d_actual_timestamp = DateTime::createFromFormat('U',$actual_timestamp);
$d_actual_timestamp actually contains :
DateTime Object
(
[date] => 2015-08-07 07:55:23
[timezone_type] => 1
[timezone] => +00:00
)
How can i get only 07/08/2015 from $d_actual_timestamp ? I need it in that specific format to query my database.EDIT Tried $d_actual_timestamp -> format('d/m/Y'); but i did not work.
thanks.
Your timestamp is stored in $d_actual_timestamp but you are trying to get a date from $d_user_timestamp.
Use $d_actual_timestamp->format('d/m/Y');
I'm building a filter for some paginated lists, and i want to be able to show the elements created between two dates. But I'm not sure how to do it correctly.
The view:
<?php echo $this->Form->create('Logs');?>
<fieldset>
<?php
echo $this->Form->input('start',array('type'=>'date'));
echo $this->Form->input('end',array('type'=>'date'));
?>
</fieldset>
<?php echo $this->Form->end('Filter');?>
The controller:
...
$conditions['Logs.created BETWEEN ? AND ?'] = array( $this->data['Logs']['start'],$this->data['Logs']['end']);
...
the problem is that $this->data['Logs']['start'] and $this->data['Logs']['end'] are arrays and i need strings:
[Logs] => Array
(
[start] => Array
(
[month] => 04
[day] => 19
[year] => 2011
)
[end] => Array
(
[month] => 04
[day] => 19
[year] => 2011
)
)
I know that i could use some php functions to transform the array into string, but there must be some function or something in cake. I feel that i'm not constructing the view correctly
Thanks for your help.
I think the best bet for you would be to write your own helper function and call it in your controller.
This thread talks about how to create your helper : How best to convert CakePHP date picker form data to a PHP DateTime object?
You can load it into the controller like so
App::import('Helper', 'DateHelper');
$dateHelper = new DateHelper();
Also, why not just switch to a regular text input and maybe a JS datepicker? This would solve your problems without having to resort to the helper route. You'd lose the dropdown availability though.
you can use the time helper, and it's daysAsSql function
daysAsSql( $begin, $end, $fieldName, $userOffset = NULL )
daysAsSql returns a string in the format "($field_name >= '2008-01-21 00:00:00') AND ($field_name <= '2008-01-25 23:59:59')". This is handy if you need to search for records between two dates inclusively.
See Time helper