PHP DateTimeZone Member Access - php

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

Related

PHP Date Object Add 3 Seconds

I want to add 2 seconds for current date object, I am getting data objects as propel foreach array result, this is the object I receive
DateTime Object
(
[date] => 2020-05-22 09:03:21.000000
[timezone_type] => 3
[timezone] => Australia/Melbourne
)
I tried this ways but no any change,
$row->getStartTime()->add(DateInterval::createFromDateString('+2 seconds'));
$row->getStartTime()->add(new DateInterval('PT2S'));
But I am unable to get the new with 2 seconds added for received time.
$time = $row->getStartTime()->format('H:i:s');
$callTimeMax = date('H:i:s', strtotime('+3 seconds', strtotime($time)));
This is my approach, It worked.

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

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 timestring to DateTime don't give correct result

I have a timestring comming from an XML File
1408226400
Now i want to convert this string into an DateTime object, but i'm getting the wrong result.
//This is an example for this Question, but with the real datestring
//My real code looks like this: $dbTurn->setStartDate(new \DateTime("#".$turn['cruiseStartDateString']));
$test = date("d-m-Y H:i", 1408226400);
$dateTime = new DateTime($test);
print_r($dateTime);
# result is DateTime Object ( [date] => 2014-08-16 18:00:00 [timezone_type] => 3 [timezone] => America/New_York )
But that is not the date i expected, because the result i should because is this:
2014-08-17 10:04:00
Also, taken from the real code i mentioned in the comment within the code, this snippet:
$dbTurn->setStartDate(new \DateTime($turn['cruiseStartDateString']));
Throws this error if i don't suppress the error message:
message => (string) DateTime::__construct(): Failed to parse time string (1408226400) at position 7 (4): Unexpected character
Is something wrong with the datestring or am i doing something wrong?
You don't need to create a formatted datestring using date() before passing to a DateTime object constructor:
$timestamp = 1408226400;
$dateTime = new DateTime('#' . $timestamp);
print_r($dateTime);
though you may also need to pass a timezone to the DateTime constructor as well
you can try this
date_default_timezone_set('Europe/Berlin');
$test = date("Y-m-d h:i:s", 1408226400);
$dateTime = new DateTime($test);
print_r($dateTime);
OUTPUT :
DateTime Object
(
[date] => 2014-08-17 12:00:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)

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