Support the ongoing development of Laravel.io →
posted 8 years ago
Database
Last updated 1 year ago.
0

The DATE type is used when you need only a date value, without a time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

stackoverflow

0

yes, thats what mysql docs says

here's the code from the migration

Schema::create('ediciones', function(Blueprint $table)
		{
			$table->increments('id');
			$table->string('date');
			$table->timestamps();
			$table->timestamp('fecha');
			$table->softDeletes();
		});

and from the model

Dates will come in a different format, so it has to be prepared for that


class Edicion extends Model {

	use SoftDeletes;

	protected $table = 'ediciones';

	protected $fillable = ['fecha','nombre','date'];

	protected $dates = ['deleted_at'];

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

and from the controller:

$fecha = $hoja->getTitle();
		if(preg_match('/[0-9]{2}[\-][0-9]{2}[\-][0-9]{4}/',$fecha) == 1)
		{
			$edicion = Edicion::firstOrNew(['fecha'=> $fecha]);
			$edicion->save();

I added regex check to see if data was coming on some weird way

After hours looking at this I got that dates before 1970 are not valid dates, all the models that came with those dates had the same issue, while those after 1970 don't and get saved correctly

For now I'm changing the data type from datatime to string, and it works, guess that will do for now, but there probably is a better a solution for this

Last updated 8 years ago.
0

I found this on google, in case anyone else finds it the issue is TIMESTAMP in mysql only goes to 1970, it needs to be set to DATETIME in the migration.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

dieg0 dieg0 Joined 30 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.