mogodb aggregate works on playground but not in php - php

I got that script on play ground that works perfect
PlayGround.
in php I got error that from my understanding its the conversation of the date to string
what can be the reason?
can i use other method instead that will work?
I'm storing date as Y-m-d H:i:s in mongodb.
the error is when subtracting $answerTime - $startTime
the error
Fatal error: Uncaught MongoDB\Driver\Exception\CommandException: Error parsing date string ''; 0: Empty string ' in...

Related

What I might be doing wrong here while converting the date?

I simply just want to convert the date in PHP. I am using PHP 7.0.33. It was working fine before but suddenly it started producing a Fatal error
Below is my code.
When I use format() I get
Fatal error: Uncaught Error: Call to a member function format() on boolean
$date = DateTime::createFromFormat('d/m/Y', $dates[$i]);
$date = $date->format("Y-m-d");
Any help will be great!!
DateTime::createFromFormat() returns FALSE in case of error, so the problem is in this function call. Check the value of $dates[$i].

I cannot convert date and time to Carbon instance in Laravel 5.5

I am trying to create Carbon instance from different $date and $time variables. I am using following code right now.
$user->created_at = \Carbon\Carbon::parse($entry->Date." ".$entry->Time);
But I am getting this error.
In Carbon.php line 547:
DateTime::__construct(): Failed to parse time string (18/08/2017 10:49:50) at position 0 (1): Unexpected character
I also tried removing $time variable to check if it works with just $date variable or not. But In that case too, I am getting this error:
In Carbon.php line 547:
DateTime::__construct(): Failed to parse time string (18/08/2017) at position 0 (1): Unexpected character
I also tried trimming out " using trim() function to make sure that the character that Carbon cannot understand is not ". But it's giving me the same output.
How can I solve this error? I tried to find on the internet and other stack overflow questions and they suggested me to update my Carbon package and I did. But still, it's giving me the same error. I can't figure out what's wrong with the code.
UPDATE: I also tried with strtotime($entry->Date); but still, Same error!
You can use this,
Carbon::createFromFormat('d/m/Y H:i:s', $entry->Date.' '.$entry->Time);
I hope this will help
Create a timestamp of date and time . After that you can parse them using Carbon
$timestamp = Carbon::createFromTimestamp($entry->Date. $entry->Time);
$dateTime=date('d/m/Y h:i:s',$timestamp);
Carbon::parse($dateTime);

DateTime::createFromFormat issue with particular date/time

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

Is it me or there's a bug in PHP DateTime?

I've just encountered on a problem I never though would exist.
I have a form that submits dates in the following format 04/28/2013 11:00. On the user front-end I'm using jquery datetimepicker and on the backend I have php to process the form.
Doing some testing I found out that DateTime in php does not throw an exception when the time is broken. For example this 04/28/2013 11:00123123 would not trigger an exception - instead DateTime returns now time. In my case the date is not related to now - it's a specific date & time in the future.
In my opinion DateTime should return an exception rather than now time. Is it me, or this is a bug?
Edit:
I'm using php 5.3.23
Posting this as an answer as the comments wouldn't fit for it.
<?php
new DateTime('04/28/2013 11:00123123');
I'm getting:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (04/28/2013 11:00123123) at position 16 (1): Double time specification' in ...
Exception: DateTime::__construct(): Failed to parse time string (04/28/2013 11:00123123) at position 16 (1): Double time specification in ...
Call Stack:
0.0001 635184 1. {main}()
0.0001 636048 2. DateTime->__construct()
I'm using PHP5.3.10. And you?

Trouble using MSSQL datetime in PHP (via SQLSRV)

I am going round in circles with this one! I'm doing the following:
Retrieving a date from an MSSQL datetime field via SQL/PHP
Sending the date to a new PHP page via the querystring
Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
echo $rSessionDate->format('Y-m-d H:i:s');
output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']);
echo date_format($rSessionDate, 'Y-m-d H:i:s');
output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx
if the date was retrieved from an MSSQL table and you want to use strtotime() in PHP and also don't want to change the date format to yyyy-mm-dd then you can use
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL

Categories