Cakephp 3 giving date and time fields in frozentime object - php

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

Related

Fetch date from Carbon\Carbon Object

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

how to change the format of date in object format array in edit entity in Cakephp 3.x

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.

get formatted date from timestamp using DateTime

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');

PHP Date Interval issue

Hi I am trying to add 90 seconds to a date stored in my DB and compare it with my current time and then take some decision. I have written following code. Also sharing response i am getting.
$curr_date_time=new DateTime('now');
$start_date_time=new DateTime($judge_obj->created_at);
$finish_date_time=$start_date_time->add(new DateInterval('PT90S'));
print_r($curr_date_time);
print_r($start_date_time);
print_r($finish_date_time);
Response:
DateTime Object ( [date] => 2014-08-11 11:40:53 [timezone_type] => 3 [timezone] => UTC )
DateTime Object ( [date] => 2014-07-25 09:43:10 [timezone_type] => 3 [timezone] => UTC )
DateTime Object ( [date] => 2014-07-25 09:43:10 [timezone_type] => 3 [timezone] => UTC )
you see there is no difference in start_date_time and finish_date_time. please help
The add() method modifies the DateTime object itself ("by reference" if you will), so calling
$start_date_time->add(new DateInterval('PT90S'));
will actually update $start_date_time
Use
$start_date_time = new DateTime($judge_obj->created_at);
$finish_date_time = clone $start_date_time;
$finish_date_time->add(new DateInterval('PT90S'));

Get month from mssql DateTime Object with php

When I print the Array I get this
[CreatedDate] => DateTime Object ( [date] => 2013-03-20 00:00:00 [timezone_type] => 3 [timezone] => America/Denver )
I'm trying to pull the month from the date, so far no luck. Been doing a combination of the below code and got a date to return but it was 12/31/1969 which is not in my database
$month = date("m",($row['CreatedDate']));
$month = date("m",($row['CreatedDate.date']));
$month = date("m",($row['date']));
Whatever produced the DateTime object already has what you need. You just need to call format() to get the month:
echo $object->format('m');

Categories