Query in Laravel not working against created_at column - php

I am writing a query in which I want to fetch data against created_at column. But its not working, it does not return anything. I am using Mongodb as database. Created_at column date is in the format "created_at": ISODate("2016-05-11T09:29:33.112Z") and when I fetch it from database it gives me 2016-05-11 09:29:33.
$datetimenow = date('Y-m-d H:i:s');
$data = ChatMessages::where('created_at','<',$datetimenow)->count();

Try using Carbon Package
$now = \Carbon\Carbon::now('Asia/Dubai'); //you can specify your own timezone
$messages = ChatMessages::where('created_at', '<', $now)->get();
dd($messages);

I got the solution, this is the way how I can do this.
$d1 = new MongoDate(strtotime('2016-01-04 23:21:46'));
$d2 = new MongoDate(strtotime('2016-05-09 23:21:46'));
$data = ChatMessages::whereRaw(['created_at' => array('$gt' => $d1, '$lt' => $d2)])->count();

Related

PostgreSQL timestamp after converting to date, return date value - 01/01/1970

After inserting data from pstgrsql database I need to convert timestamp to date.
I tried php date() function but I only got value - 01/01/1970. Here is example of my code:
$query = "SELECT * FROM \"user\" WHERE verified= 't'";
$result = pg_query($conn, $query);
while ($row = pg_fetch_row($result)) {
$date = date('d/m/Y', $row[11]);
echo "\n";
echo "$row[0] , $row[1] , $row[3], $date ";
}
$ReportRow = array('date' => $date, 'activereg' => $activerag);
$ReportRow1 = array('date' => $date);
$ReportRow2 = array('date' => $date);
$report = array($ReportRow, $ReportRow1, $ReportRow2);
*there will be more data stored in these arrays but its just work in progress for now. Thanks all for any kind responses :)
EDIT 1
DB has rows like
`3125, alex, alex#example.com, 01/01/1970`
You are using date() to try to convert a string into a date, when date only accepts a full timestamp.
date(format,timestamp);
Rather, you want to convert your string in your result into a time value. Try
$time = strtotime($row[11]);
and if you need to switch it about use
$newformat = date('Y-m-d',$time);
If you need a timestamp after that, you can
$timestamp = $time->getTimestamp(); // Unix timestamp

Mongodb ISODate format comparison not working in PHP

I have a mongodb record like "estdate" : "2016-06-14T06:49:00.000Z" I am writing the following php code to query the time
$from=$start = new MongoDate(strtotime('2016-04-11 00:00:00'));
$to=new MongoDate(strtotime('2016-06-18 00:00:00'));
$filterpatient = array("order.patientinfo.order_id" => $ordertext,"order.orderitem.estdate" => array('$gte' =>$from, '$lte' => $to));
$cursor = $collection->find($filterpatient)->sort(array('_id'=>-1));
But this is giving no result.
If you are using mongodb driver, this might be helpful for you.
$starttime = strtotime("2016-04-11 00:00:00");
$utcstartdatetime = new MongoDB\BSON\UTCDateTime($starttime);
Now use the $utcstartdatetime in your query for time.

Inserting a date in mongodb

I want to insert a date into a collection.
I use the class MongoDate to create the date object:
$today = new MongoDate(strtotime(date('Y-m-d 00:00:00')));
The problem is that once it is in my collection the date is 2 hours earlier.
For instance, $today here should be 2013-05-28 00:00:00 but once in the database it is 2013-05-27 22:00:00.
I can't resolve this problem by adding 2 hours manually to the timestamp because I use the date in queries.
The local time of the server where Mongo is running is set to the correct time of my country.
That works in the new php version of mongodb:
new MongoDB\BSON\UTCDateTime((new DateTime($today))->getTimestamp()*1000)
$dt = new DateTime(date('Y-m-d'), new DateTimeZone('UTC'));
$ts = $dt->getTimestamp();
$today = new MongoDate($ts);
This is working.
Remove old document and insert
$bill = array(
"_id" => 1,
"name" => "A",
"lastModified" => new MongoDate()
);
$collection->insert($bill);
FYI: If you need date created for your object model.
$date_created = new \MongoDB\BSON\UTCDateTime(time()*1000);

Returning months difference as integer

I am trying to return month difference as integer and call if to table column. My code:
function ETA($ArrivalDate, $pattern = 'mysql'){
$patterns = array(
'eu' => 'd/m/Y',
'mysql' => 'Y-m-d',
'us' => 'm/d/Y',
);
$CurrentDate = date("Y-m-d");
$ArrivalDate = $variants_data['ArrivalDate'];
$diff = $ArrivalDate->diff($CurrentDate);
return $diff->y;
}
The I call it with
<td>'.$_GET['ETA'].'</td>
But there is nothing returned, what am I doing wrong here?
You can get what you want in a simpler approach using the DateTime object in PHP:
function ETA($ArrivalDate){
$currentDate = new DateTime();
$arrivalDate = new DateTime($ArrivalDate);
$interval = $currentDate->diff($arrivalDate);
return $interval->format('%m');
}
See working example: http://3v4l.org/UlPQo
If you don't pass in an appropriate format $ArrivalDate you will get an exception thrown, so you need to wrap the call in a Try/Catch.
See DateTime Interval Format for more on the return value.
Try this simple one
<?php
//for months
$monthdiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24*30)));
//for days
$daydiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24)));
?>

Mongo date range query using _id in PHP

Being relatively new to mongo, I read in the mongo manual about Optimizing Object IDs.
http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Extractinsertiontimesfromidratherthanhavingaseparatetimestampfield.
Going with the recommendation about NOT creating a separate Created_On field, I decided I would later extract out the date from the _id field which is an ObjectID
Now, I have many records all with out a Created_On field.
I am attempting to query for a date range, but am unsure of the syntax:
$start = new MongoDate(strtotime("2012-03-01 00:00:00"));
$end = new MongoDate(strtotime("2012-03-15 00:00:00"));
$collection->find(array('_id' => array('$gt' => $start, '$lte' => $end)));
Always returns 0 results. Although I can query individual records and extract the date from the object.
In PHP, you need to do something like:
function timeToId($ts) {
// turn it into hex
$hexTs = dechex($ts);
// pad it out to 8 chars
$hexTs = str_pad($hexTs, 8, "0", STR_PAD_LEFT);
// make an _id from it
return new MongoId($hexTs."0000000000000000");
}
$start = strtotime("2012-03-01 00:00:00");
$end = strtotime("2012-03-15 00:00:00");
$collection->find(array('_id' => array('$gt' => timeToId($start), '$lte' => timeToId($end))));
Then you can use that to query the _id field.
I wrote a blog post describing the process here: http://www.snailinaturtleneck.com/blog/2011/12/20/querying-for-timestamps-using-objectids/

Categories