首页 > 解决方案 > 如何添加一天并同时指定该日期的静态时间?

问题描述

好我有一个问题,希望你能帮助我,事实是我必须从我的数据库中获取一些时间戳并添加它们一天,好吧,好吧,我使用 carbon addday(),但是由于 carbon 确实不想比较我的时间戳然后我必须转换为字符串,通过解析我可以将它们同化为碳,但在那个过程中我的日期发生了变化,日期是正常的,但由于某种原因,时间加起来是几个小时和这完全改变了我正在寻找的结果,我该如何解决?我通过我的命令:

<?php

namespace App\Console\Commands;

use App\p;
use App\Pe;
use App\Mail\SendMailable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
class LimpiarDiario extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Task:LD';

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

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       $datedi = p::select('p.Date', 'p.coddate')
               ->where('p.cod_paperiod', '=', 1)
               ->get();
           foreach ($datedi as $dateda)
            {
                $cod = $dateda->coddate;
           $now = Carbon::now();
            $dateli = $dateda->Date;
            $dateinter = strtotime($dateli);
           $datecar = Carbon::Parse($dateinter);
           $datetrans = $datecar->addDay();
           $datenue= $datetrans->toDateTimeString();
           if ($dateda->Date < $now)
            {
           $prueba= p::where('coddate','=', $cod)
           ->update(['Date' => $datenue]);

           }

           }
        }

为了显示问题,我的 dateda 的结果是:{"Date":"2020-06-12 16:00:00.000000","coddate":53} 然后它变成 dateli 并且它的值是:2020-06-12 16:00:00.000000 它再次变成 dateinter 并且它的值是:1591992000 最后它变成带有变量 datecar 的碳:"2020-06-13T20:00:00.000000Z" 并且由于某种原因你现在可以看到它将原始时间增加 4 小时,因此当我注册结果时,它会为我保存日期:2020-06-13 20:00:00

当期望的结果应该是:2020-06-12 16:00:00

因此,正如所解释的,我必须寻求帮助才能知道如何设置解析时间,或者如何在不更改时间的情况下将其转换为时间戳。

标签: laravelphp-carbon

解决方案


为什么所有这些脚本只是为了做出更新声明......

$now=Carbon::now();

p::where('p.cod_paperiod', '=', 1)->where('date','<',$now)
    ->update(['Date' => DB::raw("Date + INTERVAL 1 DAY")]);

推荐阅读