DateTime::createFromFormat issue with particular date/time - php

I have been using the following code for a couple of months and it's been working fine, but now seems to have an issue with a certain time or time range.
I have a GPS tracker that sends it's date/time like this:
150102235335
The format is ymdhis
$input_array[6] = 150102235335;
$datetime = DateTime::createFromFormat('ymdhis', $input_array[6]);
$datetime = $datetime->format('Y-m-d h:i:s');
Using that time, php crashes with the following error:
PHP Fatal error: Call to a member function format() on a non-object
It seems that datetime ends up empty.
However, using the time 150103004933 works just fine.
Can anyone see where I have gone wrong here, or is this a bug?
Is there a better way to accomplish my date conversion?
I am using PHP 5.4.35

h is 12-hour format; and therefore 23 hours is invalid. Only values in the range 00-11 would be valid.
H is 24-hour format

Related

The separation symbol could not be found Data missing

I'm working with Laravel 5.8 and I wanted to show a popup message if the UNIX timestamp of the current date is equal to the defined Unix timestamp of the popup.
So in order to do that, I added this at the Controller:
$date1= $popup->datep; // returns 1636403400
$date1 = Carbon::createFromFormat('Y-m-d H:i:s', $date1);
dd($date1);
But instead of getting the result of $date1, I get this error:
The separation symbol could not be found Data missing
So what's going wrong here? How can I solve this issue?
You are specifying a format that is clearly not an unix timestamp. Use method for the timestamp.
$date = Carbon::createFromTimestamp($popup->datep);
If you want to compare it to be the same date, you should do the following. I don't assume you want to compare it by the hour or second, that those will almost never match.
$date->startOfDay()->eq(now()->startOfDay());
Regarding Carbon Docs:
createFromFormat() is mostly a wrapper for the base php function DateTime::createFromFormat.
which is means that your second parameter must be a valid date/time format, not a timestamp.
The DateTime::create docs:
$datetime
String representing the time.
Instead, you need to use the createFromTimestamp instantiator.
$date1 = Carbon::createFromTimestamp($date1);

inject date string as srtotime

I am trying to resolve this since hours, and I tried different solutions offered here within the website, but for some reasons the website always returns an error when applying the suggestions.
I have a form field which uses a datepicker, and the output is a date in the format: November 6th, 2013
Now, the mysql database and the rest of the script only works with a ten digit date format, which I have been told is the unix timestamp format and that it can be converted using the strtotime. So I am trying to convert the string into strtotime, but cant resolve this. For now I have:
$insertData['enddate'] = $this->input->post('openDays');
which returns the date "November 6th, 2013" and I changed it to
$insertData['enddate'] = strtotime $this->input->post('openDays');
which returns the error: Parse error: syntax error, unexpected T_VARIABLE in /home/xxx/public_html/app/controllers/project.php on line 214
any suggestions on how to correctly apply this?
Thanks
Try this, you are doing a mistake by using strtotime.
$insertData['enddate'] = strtotime($this->input->post('openDays'));
Reference link: Strtotime PHP
strtotime is a function , so you need to enclose it like this (using parenthesis)
$insertData['enddate'] = strtotime($this->input->post('openDays'));
-------^ -------^
EDIT :
I suggest you use DateTime instead of strtotime
$dt = DateTime::createFromFormat('H:i', '17:00');
$insertData['enddate']=$dt->format('H:i');

Time format conversion

I need to convert a specific date format into local time (Europe/Sarajevo), I have the time in this format 2013-02-17T15:00:00Z which I don't really understand and this is why I don't know how to convert it to the Europe/Sarajevo time, who knows maybe it is already Sarajevo time, I don't know...
OK I can parse it and remove the T and Z and get a time but these letters mean something, probably they affect the result time...
The result time is for example 2013-02-17 18:00:00, probably there will be a difference due to the letters T and Z which are probably time offset.
Use DateTime. It's much better for working with timezones:
$datetime = new DateTime('2013-02-17T15:00:00Z');
$datetime->setTimeZone(new DateTimeZone('Europe/Sarajevo'));
echo $datetime->format('c');
Reference
DateTime
DateTimeZone
See it in action
You can use php date function like this
$date = '2013-02-17T15:00:00Z';
echo date('Y-m-d H:i:s',strtotime($date));
See the Manual

PHP date time to Unix time Stamp

How can I convert string "M-d-Y::H:i:s" to unix timestamp in php. I just realized manipulating date and time must be the most time consuming entity in any programming language.
Trivial Question: Why do you think there isn't any universal date time format. Why are there so many variations of same data? AArrhh.
The OOP way (requires PHP >= 5.3.0):
$dt = DateTime::createFromFormat("M-d-Y::H:i:s", $input);
$ts = $dt->getTimestamp();
You should of course check the return value of createFromFormat (it's false if an error occurs) but you should also definitely check DateTime::getLastErrors(); otherwise you might be surprised if e.g. your input has a day of "Jan 32". See my answer here for more info.
i have this issue service unavailable (with message) ResponseText: Error: Call to a member function getTimestamp() on boolean How to fixed this issue.

Call to undefined method DateTime::setTimeStamp()

I've been using setTimeStamp to convert a Unix Timestamp to a datetime in the following way:
$startHireConverted = strtotime($startHire); // converts start hire to time
$endHireConverted = strtotime($endHire); // converts end hire to time
$startdt = new DateTime();
$startdt->setTimeStamp($startHireConverted);
$mysql_startdate = $startdt->format("Y-m-d H:i");
This was working nicely, but recently I've put the website live and the version of PHP can only be 5.2.12 which doesn't support the setTimeStamp method.
I've tried changing setTimeStamp to format which is getting rid of the errors and converting the datetime but it is changing it to the current datetime - 5 hours for some reason rather than the date stored in $startHire.
$startdt->format($startHireConverted);
Any ideas on how to get around this problem?
$startHire starts out as a string version of datetime.
Thankyou
I don't think you need to do the step with unix timestamp at all:
$startdt = new DateTime($startHire);
PHP manual: DateTime::__construct
But that might depend on the format you're getting $startHire in. See Supported Date and Time Formats.

Categories