Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

968
Views
Compare la fecha con la fecha ISO de mongodb en laravel

Estoy usando mongodb con laravel y he almacenado la fecha en formato ISO en la colección mongodb como se muestra a continuación

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

Ahora, estoy tomando fechas del usuario y quiero obtener el registro entre las 2 fechas dadas. Intenté esto con los siguientes métodos, pero ninguno de ellos funcionó en mi caso. Método 1:

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

Método - 2:

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

Ya hay preguntas similares disponibles en el desbordamiento de pila y también probé algunas de las soluciones, pero nada funciona en mi caso. No se genera ningún error y tampoco obtengo el resultado requerido. Entonces, ¿alguien puede ayudarme? Gracias por adelantado.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En el paquete Laravel Carbon puede ayudarnos a manejar la fecha y la hora, así que simplemente use la función Carbon para convertir su fecha y hora como se muestra a continuación.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!