Detect whether a time was specified in a DateTime object - php

I have a list of dates, some with times:
01/03/2014
02/01/2014 02:00 PM
02/02/2014 1:00 PM
03/01/2014
I am going through all of these and turning them into new DateTime objects, sending them to JavaScript, then using moment.js to display them.
moment(data.date).format('L LT')
This displays the dates in the following format:
01/03/2014 12:00 AM
02/01/2014 02:00 PM
02/02/2014 01:00 PM
03/01/2014 12:00 AM
Is there a way to detect, without regex (or whatever string manipulation), that the DateTime constructor was called with just a date (without a time)?

I don't think DateTime class provides a way to detect if the object was actually created from just a date. That information is lost once the object is created. A small-work around is to use date_parse() to check for absent date time fields (as #salathe mentioned in the comments).
If the DateTime doesn't contain a hour, minute or second part, then it means it only has a date part in it. Using the above logic, your code would look like:
$datetimes = [
'01/03/2014',
'02/01/2014 02:00 PM',
'02/02/2014 1:00 PM',
'03/01/2014'
];
$withTime = array();
$withoutTime = array();
foreach ($datetimes as $dt) {
$parts = date_parse($dt);
if ($parts['hour'] === FALSE &&
$parts['minute'] === FALSE &&
$parts['second'] === FALSE) {
$withoutTime[] = $dt;
} else {
$withTime[] = $dt;
}
}
print_r($withTime);
print_r($withoutTime);
Outputs:
Array
(
[0] => 02/01/2014 02:00 PM
[1] => 02/02/2014 1:00 PM
)
Array
(
[0] => 01/03/2014
[1] => 03/01/2014
)
Demo

Related

Sugarcrm date not showing properly

SCENARIO
So I've custom object where I've three fields called start_date, end_date and booking_expire_time(for an event).
My dates are coming from Magento in form of String ofc, I already have logged so they are coming like
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $start_date - 08/30/2018 11:59 AM
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $event_endt_time - 08/31/2018 11:59 AM
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $booking_expire_time - 08/30/2018 12:59 PM
Which is correct and same which I've chosen in Magento
And I'm storing it like following in that bean.
$st = date_create_from_format("m/d/Y H:i A",$start_date);
$eventBean->start_date = date_format($st, 'Y-m-d H:i:s');
$en = date_create_from_format("m/d/Y H:i A",$event_end_time);
$eventBean->event_end_time = date_format($en, 'Y-m-d H:i:s');
$ex = date_create_from_format("m/d/Y H:i A",$booking_expire_time);
$eventBean->booking_expire_time = date_format($ex, 'Y-m-d H:i:s');
ISSUE
There is no problem for saving except when I go to the UI and check date and time, the date and time are different.
I tried changing user timezone and storing it again but it is still same, I tried setting the timezone to UTC in User settings but still, the same thing is happening.
I've logged the date time which getting retrieved in code and it prints following for start date and end date
DateTime Object
(
[date] => 2018-08-30 11:59:00.000000
[timezone_type] => 3
[timezone] => UTC
)
DateTime Object
(
[date] => 2018-08-31 11:59:00.000000
[timezone_type] => 3
[timezone] => UTC
)
EDIT
Timezone setting
PHPInfo from Diagnostic Tool

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?

Combining an Time / Date Array to convert to UNIX TIME

So I have a form for creating scheduled dates.
Title, Subject, bla bla...
But then, I have a jQuery Date picker, that lets the user pick a date off a calendar.
The jQuery date picker only formats for human dates
I want to store them in UNIX TIME.
So I have this Calendar, for the YEAR, MONTH, DAY...
Then I have a standard drop down for the hour, 1:00 PM, 1:30 PM Etc...
The post print_r($_POST); looks like this,
[time] => Array
(
[0] => 5:00 PM
[1] => 1:00 PM
[2] => 8:00 PM
)
[date] => Array
(
[0] => 2014-05-08
[1] => 2014-04-04
[2] => 2014-03-28
)
I found strtotime(); for converting, human time / date into UNIX TIME, however...
How do I get array [0] from time, and date to combine and be a combined string.
There might be only 1 date, or 8 dates?!
You can iterate through your POST data and combine times:
foreach($_POST['date'] as $i => $date) {
$timestamp = strtotime($date.' '.$_POST['time'][$i]);
}
$count = count($_POST['date']); // assuming both date and time have same amount of values
for ($i = 0; $i < $count; $i++) {
$time = strtotime($_POST['date'][$i] . ' ' . $_POST['time'][$i]);
// do what you want with the time here
// Example: put all timestamps in an array.
$timestamps[] = $time;
}

PHP Finding how many hours of a timespan is within another timespan

I am trying to find a way to determine whether a timespan is partially or fully within another timespan. For example:
I have time entries of:
Monday 18:30:00 to Tuesday 05:00:00,
Monday 23:00:00 to Tuesday 05:00:00,
Monday 20:00:00 to Tuesday 08:00:00,
Monday 00:00:00 to Tuesday 08:00:00,
and need to find, for each one, how much of the time is within a timespan of 22:00:00 to 06:00:00. The output would need to be:
07:00:00,
06:00:00,
08:00:00,
06:00:00.
What you're looking for is DateTime::diff which returns an instance of an DateInterval
Here a little example:
$d1 = new DateTime("Monday 18:30:00");
$d2 = new DateTime("Tuesday 05:00:00");
$limit1 = new DateTime("Monday 22:00:00");
$limit2 = new DateTime("Tuesday 06:00:00");
$within1 = $d1->getTimestamp() < $limit1->getTimestamp() ? $limit1 : $d1;
$within2 = $d2->getTimestamp() < $limit2->getTimestamp() ? $d2 : $limit2;
$interval = $within1->diff($within2);
print_r($interval);
print_r($interval);
outputs:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 7
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
You can either read this attributes directly or you can use the DateInterval::format()

filtering by date php for this month and last x days and this week

I have a date that returns in a string as 2012-03-19 05:00:32, its not coming from the database
I can use below to search for the last 30 days
$date = '2012-03-19 05:00:32';
if (strtotime($date) >= strtotime('-7 days')) {
// do something
}
Problem is if today is the 19th March, i was to search from the 11th to the 18th for the last 7 days and that seems to search for the last 7 days by calculating 24 hours * 7 by my searches need to start from 00:00:01 each day.
My plan is to break the date down into Year, Month and Day then check if year = 12, then check if month = 3, then check if date between 11 and 18.
Im just wondering if there is a more efficient way to do this or if im on the right track.
I also have the same issue with running a search on all info from this month and also want to search for all info this week starting on Monday.
So this is just asking if my method is sound or if there is a more efficient method.
$mytime = new DateTime('2012-03-19 05:00:32');
$mydate = new DateTime($mytime->format('Y-m-d')); //keep date only, exclude the time component
$now=new DateTime; //includes hours, minutes, seconds
$today=new DateTime($now->format('Y-m-d')); //time set to 0:00
$interval = $mydate->diff($today);
if($interval->format('d') <=7) { //assuming that $mydate isn't in the past
//do something
}
I'd suggest to use DateTime class ...
<?php
$d1=new DateTime("2012-07-08 11:14:15.638276");
$d2=new DateTime("2012-07-08 11:14:15.889342");
$diff=$d2->diff($d1);
print_r( $diff ) ;
/* returns:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
*/
?>

Categories