get formatted date from timestamp using DateTime - php

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

Related

PHP Timezone bug: adding extra hour

I have problem...
I'm currently working with "America/Santiago" timezone, and whenever I create a datetime object with a hour < 01:00AM, it adds 1 hour.
Example:
date_default_timezone_set('America/Santiago');
$dateTest = date_create_from_format('d/m/Y H:i:s', '04/09/2022 00:32:27');
print_r($dateTest);
This prints:
DateTime Object
(
[date] => 2022-09-04 01:32:27.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
But if I create the following object:
date_default_timezone_set('America/Santiago');
$dateTest = date_create_from_format('d/m/Y H:i:s', '04/09/2022 01:22:11');
print_r($dateTest);
prints:
DateTime Object
(
[date] => 2022-09-04 01:22:11.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
I'm really lost here, can someone guide ?
Thanks

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.

Cakephp 3 giving date and time fields in frozentime object

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

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