如何使用 Laravel 将 firebase 中的日期与当前日期进行比较

How to compare the date in firebase with the current date using Laravel

提问人:Pamy Pamly 提问时间:9/2/2023 最后编辑:Pamy Pamly 更新时间:9/3/2023 访问量:74

问:

请不熟悉使用 Laravel 和 firebase。请问我如何将 firebase 中的日期与当前日期进行比较,我想检查我的 firebase 中插入的日期是否大于或等于当前日期。以下是我所做的,但它不起作用,请提供任何帮助将不胜感激。

注意:这是我在 firebase 中的日期格式“2023-08-31”

我正在运行一个 cron 作业,该作业检查任何插入用户的当前日期,一旦 cron 作业检查日期,如果当前日期大于插入日期,则将借记用户。我的 cron 作业运行良好,但由于我启动的日期,它没有从用户中扣除。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;


use DateTime;

use Illuminate\Support\Facades\Route;
use App\Http\Middleware\VerifyCsrfToken;
use Illuminate\Http\Request;

class DailyChargez extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'DailyChargeUserz:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        
        \Log::info("Cron is working fine daily!");
        
        


$mydate = Carbon::now();


$daily = app('firebase.firestore')->database()->collection('Payment');
$query1 = $daily->where('paymentType', '==', 'Recurring Payment')
->where('active', '=', true)
->where('frequence', '==', 'Daily')
->where('deductionDate', '<=', $mydate)


$documents = $query1->documents();
foreach ($documents as $document) {
    if ($document->exists()) {
       
    $postDataz = [
     'authorization_code' => $document->data()['authenticate'],
    'email' =>  $document->data()['email'],
    'amount' => $document->data()['amountToCharge'] * 100,
      
      "metadata" => [
            "custom_fields" => [
         [
                "value" => $document->data()['paymentMethod'],
                "display_name" => "Payment Method", 
               "variable_name" => "payment method", 
         ], 
        [
             "value" => $document->data()['packageName'],
                "display_name" => "Package Name", 
               "variable_name" => "package name", 
            ],
             
         
         
      ]
      ]
     
     
      ];
      
    }
    
    

      
        $url = "https://api.paystack.co/transaction/charge_authorization";
  $fields_stringz = http_build_query($postDataz);
  //open connection
  $ch = curl_init();
  
  //set the url, number of POST vars, POST data
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_POST, true);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_stringz);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer *******",
    "Cache-Control: no-cache",
  ));
  
  
  //So that curl_exec returns the contents of the cURL; rather than echoing it
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
  

  $result = curl_exec($ch);
   curl_close($ch);
     $info = json_decode($result);
     
  
       
    
}    
    }
}

php laravel firebase 日期时间 google-cloud-firestore

评论

0赞 Tim Lewis 9/2/2023
“但它不起作用”——具体来说,什么不起作用?你遇到任何错误吗?正在发生的事情与你期望发生的事情?请编辑您的问题并提供更多详细信息。
0赞 Pamy Pamly 9/2/2023
对不起,我刚刚更新了它。
0赞 Tim Lewis 9/2/2023
这是一个更好的描述,但没有代码来支持你所期望的......您的方法对 没有任何作用,所以当您说“由于我发起的日期,它没有从用户中扣除”时,我必须回答“为什么会这样?没有超越的代码......另外,仔细检查 Laravel 的 Collection 类的方法:laravel.com/docs/10.x/collections#method-where。 筛选集合,但它不返回 或 ,它返回另一个集合。你需要做点什么......handle()$query1$query1 = ...where()where()truefalse
0赞 Pamy Pamly 9/2/2023
@TimLewis 我正在使用 cron 作业运行定期付款,该作业检查 firebase 以查看是否有任何用户日期是向后的,而不是将来的日期,也就是说,如果 A 先生的日期是 2023-08-31,系统将检查当前日期并借记用户,因为当前日期大于 A 先生的日期
0赞 Tim Lewis 9/2/2023
另外,做一些测试和调试; ( 是 ,你可以改用 )是 ,因为使用字符串比较,并且是一种可以很容易地比较的格式。但同样,除非您省略了其他代码,否则您不会对 做任何事情。'2023-08-31' <= Carbon::now()Carbon::now()'2023-09-01 15:32:00'Carbon::today()trueCarbonYYYY-MM-DD$query1

答:

0赞 Adrian Maxwell 9/2/2023 #1

Firestore 将日期存储为时间戳,并且您正在将这些日期与可能无法正常工作的 Carbon 日期对象进行比较。要将 Carbon 日期转换为 Firestore 时间戳,您可以执行以下操作:

// Convert Carbon date to Firestore Timestamp
$mydate = new \Google\Cloud\Core\Timestamp(Carbon::now());

$daily = app('firebase.firestore')->database()->collection('Payment');
$query1 = $daily->where('paymentType', '==', 'Recurring Payment')
    ->where('active', '=', true)
    ->where('frequence', '==', 'Daily')
    ->where('deductionDate', '<=', $mydate);

// ... rest of your code

安装 google/cloud-firestore 软件包以使用 Firestore 时间戳。在上面的脚本中包含以下内容use Carbon\Carbon

use Google\Cloud\Firestore\FirestoreClient;

评论

0赞 Pamy Pamly 9/2/2023
请@paul我是使用它的新手,当您说我应该安装 Gogle/cloud-firestore 包以使用 firestore 时间戳时,我感到很困惑。目前,在我的网站中,我可以运行 CRUD,所以既然我可以运行 CRUD,这意味着我已经安装了 google/cloud-firestore 包,对吗?因为当你说我应该安装 google/cloud-firestore 包时,我有点困惑?
0赞 Pamy Pamly 9/2/2023
同时,我已经按照你上面所说的做了,但它不起作用,请不要生气,是否缺少什么,或者更好的是,你能用我上面的完整代码向我展示它的方式吗?我将不胜感激
0赞 Pamy Pamly 9/2/2023
请我刚刚将其添加到上面的代码中,我不知道它是否已安装,请“使用 Carbon\Carbon”并在我的命令中运行我的代码,即 App/Console/Command/,所以你能让我通过吗?
0赞 Adrian Maxwell 9/2/2023
"它不起作用“ - 具体来说,什么不起作用?你遇到任何错误吗?(建议使用Google获取安装说明)
0赞 Adrian Maxwell 9/2/2023
重新安装。您可能已经完成了此操作。看看这些小评论 firebase-php.readthedocs.io/en/stable/cloud-firestore.html 不可能提供学费。(我没有被冒犯,也无意引起冒犯。