get date from DateTime Object - php

After more than an hour struggling and trying I'd like to ask it here.
Trying to make something with weeks etc. in php I got from you site this:
Get all Work Days in a Week for a given date
Nice and will work for me fine.
But ... I can't get, trying and trying, the data out of this part: [date] => 2013-08-12 00:00:00
Array
(
[0] => DateTime Object
(
[date] => 2013-08-12 00:00:00
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
How to get that date out of the array ?
Please help me out, thanks in advance for the help !

Use DateTime::format()
$dateTime = new DateTime('2013-08-12 00:00:00');
echo $datetime->format('Y-m-d'); // produces 2013-08-12

$firstMondayThisWeek= new DateTime('2013-08-12');
$firstMondayThisWeek->modify('tomorrow');
$firstMondayThisWeek->modify('last Monday');
$nextFiveWeekDays = new DatePeriod(
$firstMondayThisWeek,
DateInterval::createFromDateString('+1 weekdays'),
4
);
$dateTimes = iterator_to_array($nextFiveWeekDays);
foreach ($dateTimes as $dateTime) {
echo $dateTime->format('Y-m-d H:i:s');
}

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.

How to display only date

$date_from1 = new DateTime('1-9-2016');
$thisdate=$date_from1->add(new DateInterval('P1D'));
when I print_r($thisdate);
I am getting the output
DateTime Object ( [date] => 2016-09-02 00:00:00 [timezone_type] => 3 [timezone] => Australia/Sydney )
Expected Output:-
2016-09-02
You need to mention the date format.
Refer to the manual: DateTime::format Example
echo $thisdate->format('Y-m-d H:i:s');
Based on date-time format manual:- http://php.net/manual/en/datetime.format.php
<?php
$date_from1 = new DateTime('1-9-2016');
$thisdate=$date_from1->add(new DateInterval('P1D'));
//echo "<pre/>";print_r($thisdate);
echo $thisdate->format('Y-m-d');
Output:-
https://eval.in/634751
check this
$date_from1 = date("d/m/Y", strtotime("1-9-2016"));

Strange bug of date_modify function in DateTime class

I use the native PHP DateTime class for adding days to dates. But when dealing with negative dates, I encountered a strange bug. Depending on the millennium added or a day or two. Example:
$date_one = date_create("-1000-12-27");
date_modify($date_one, '+1 day');
//Return DateTime Object ( [date] => -1000-12-29 00:00:00 )
$date_two = date_create("-2000-12-27");
date_modify($date_two, '+1 day');
//Return DateTime Object ( [date] => -2000-12-28 00:00:00 )
$date_three = date_create("-3000-12-27");
date_modify($date_three, '+1 day');
//Return DateTime Object ( [date] => -3000-12-29 00:00:00 )
That is, depending on the parity of the millennium issue, or December 28 or December 29. Why is this happening? What is the problem?

PHP Date: Every monday next 3 weeks (Google Calendar remake)

I'm making a CRM-system, and I now need to offer people to make recurring events. For that they will have to fill out if it's daily, weekly, monthly or yearly, and everything like when you do it in Google Calendar.
But how will I get the dates in an array for "monday next 3 weeks" for example?
$int_count = 3; // How many to repeat
$date = new \DateTime('next monday');
$result = array($date->format('Y-m-d'));
for ($i=1; $i<$int_count; $i++) {
$result[] = $date->modify('+1 week')->format('Y-m-d');
}
print_r($result);
Result:
Array
(
[0] => 2015-01-26
[1] => 2015-02-02
[2] => 2015-02-09
)
The best way is to use DateTime and DatePeriod classes. It's the most correct way to deal with dates now. It deals with timezones and DST shifts automatically. It's just the way you must do it.
$daterange = new DatePeriod(new DateTime('next monday'), new DateInterval('P1W'), 2);
$dates = [];
foreach($daterange as $date) $dates []= $date->format("Y-m-d H:i:s");
print_r($dates);
The result will be:
Array
(
[0] => 2015-01-26 00:00:00
[1] => 2015-02-02 00:00:00
[2] => 2015-02-09 00:00:00
)

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