简体   繁体   English

如何从输入中输入mysql中的时间戳

[英]how to enter time-stamps in mysql from an input

I need to insert Timestamp in MySQL from input in Laravel application 我需要在Laravel应用程序的输入中插入MySQL中的Timestamp

data coming from my input is in this format 12/27/2017 MM/DD/YYYY how can I convert it into this format Ymd H:i:s 来自我输入的数据采用这种格式12/27/2017 MM/DD/YYYY如何将其转换为此格式Ymd H:i:s

any alternatives are highly appreciated like pass data from the input which input we use so, I can get data in timestamp format. 任何替代方案都非常受欢迎,比如来自我们使用的输入的传递数据,我可以获得timestamp格式的数据。

and at the end, I need to sort data order by date 最后,我需要order by date排序数据

If you want to do it in Laravel way you can use mutator and Carbon . 如果你想用Laravel方式做,你可以使用mutatorCarbon This is an example of model: 这是一个模型的例子:

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model

class Post extends Model {

    protected $dates = ['date'];


    public function setDateAttribute($value)
    {
        $this->attributes['date'] = Carbon::createFromFormat('d/m/Y', $value);
    }
}

Now when you update or create post date attribute will be automatically converted. 现在,当您更新或创建发布date属性时,将自动转换。 So you can save it like this: 所以你可以像这样保存它:

$post = new Post(); 
$post->date = '16/12/2017';
$post->save();

You can use DateTime: 您可以使用DateTime:

$inputDate = \DateTime::createFromFormat('m/d/Y', '12/27/2017');
$formatedDate = $inputDate->format('Y-m-d H:i:s');

As for me I done date conversion in this way, for example to making invoices. 至于我,我以这种方式完成了日期转换,例如发票。 I hope this can be done by PHP. 我希望这可以通过PHP完成。

$input_date = "12/15/2017"; // input in MM/DD/YYYY
$output_date = date('Y-m-d H:i:s',strtotime($input_date)); //set output to 2017-12-15 00:00:00
echo $output_date; //produce output

It generates the following result 它会生成以下结果

2017-12-15 00:00:00

I hope this will work. 我希望这会奏效。 If you want only date, you can omit H:i:s based on your purpose. 如果您只想要日期,可以根据您的目的省略H:i:s。 Thank you. 谢谢。

$date=date_create("12/27/2017");
$rawDate =  date_format($date,"Y/m/d");// Produced - 2017/12/27
echo  str_replace('/','-',$rawDate ); // Output - 2017-12-27
  • to add H:i:s , if there is no time just add 00:00:00 at the end of date begins with space. 添加H:i:s ,如果没有时间,则在日期结束时添加00:00:00以空格开头。

  • If you're dealing with created_at or updated_at which Laravel create for every table, you must add 00:00:00 to end of date to get all data begins with that respective date 如果您正在处理Laravel为每个表创建的created_atupdated_at ,则必须将00:00:00添加到日期结束,以便从相应日期开始获取所有数据

This solution worked for me if we want the format of date from input is different from the format the timestamp accepts 如果我们想要输入的日期格式与时间戳接受的格式不同,这个解决方案对我有用

Laravel way of doing is Laravel的做法是

  protected $fillable = ['organization_name', 'job_apply_link', 'job_description', 'city_id', 'province_id', 'sector_id', 'image_file', 'test_id', 'newspaper_id', 'catagory_id', 'slug', 'meta_keywords', 'meta_description', 'job_title', 'created_at'];  

 public function setCreatedAtAttribute($value)
    {
        $inputDate = \DateTime::createFromFormat('m/d/Y', $value);
        $formatedDate = $inputDate->format('Y-m-d H:i:s');
        $this->attributes['created_at'] = $formatedDate;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM