Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

974
Vistas
Compare date with mongodb ISO date in laravel

I am using mongodb with laravel and I have stored date in ISO format in mongodb collection as below

"created_at" : ISODate("2021-09-03T05:48:44Z")

Now, I am taking dates from user and want to fetch the record between the given 2 dates. I tried this with following methods but none of them working in my case. Method - 1:

$stDate = date("Y-m-d\TH:i:s", strtotime('September 2, 2021');
 $stDate = new MongoDate(strtotime($stDate));
 $edDate = date("Y-m-d\TH:i:s", strtotime('September 7, 2021');
 $edDate = new MongoDate(strtotime($edDate));
 $query = $query->whereBetween('created_at', [$stDate, $edDate]);

Method - 2:

$stDate = date("Y-m-d\TH:i:s", strtotime('September 2, 2021');
 $stDate = new \MongoDB\BSON\UTCDateTime(strtotime($stDate));
 $edDate = date("Y-m-d\TH:i:s", strtotime('September 7, 2021');
 $edDate = new \MongoDB\BSON\UTCDateTime(strtotime($edDate));
 $query = $query->whereBetween('created_at', [$stDate, $edDate]);

There are similar questions already available on stack overflow itself and I tried some of the solutions as well but nothing is working in my case. There is no any error generating and I am not getting required output as well. So, can anyone please help me out. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In Laravel Carbon package can help us to deal with date-time so simply use the Carbon function to convert your date and time like below.

use Carbon\Carbon;

$stDate = new Carbon('September 2, 2021');
$edDate = new Carbon('September 7, 2021');
$query = $query->whereBetween('created_at', [$stDate, $edDate]);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda