Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stevenjeffries's avatar

Checking Carbon Timestamps for Null

I'm trying to detect if certain things have been processed by checking them against timestamp fields, but the fields that have not been assigned a value (that should be null) are coming up as '-0001-11-30 00:00:00'.

I'm assuming that since I have my timezone set to America/Phoenix, that when I pull the value from the model, Carbon is converting it as if it were '0000-00-00 00:00:00' into my timezone.

Now, I could hard-code it in so that I can compare the string '-0001-11-30 00:00:00', but if the timezone ever changes (e.g. a client on the east coast, or another country wants dates to show up in their local time), then I'll have to go through many parts of my code and change this as well.

Alternatively, I could remove the date from my $dates array so that it doesn't go through Carbon, but this is also not ideal.

So, my question is this: Can I have my cake and eat it too? (Can I use Carbon AND check for null dates?)

In case you want a visual representation of what I'm talking about:

I have a table, we'll call it Example because I'm creative and it will be less confusing.

Example has a field, last_processed. It was created like so:

// Migration
Schema::table('examples', function(Blueprint $table) {
    // ...
    $table->timestamp('last_processed')->nullable();
});

// Class
class Example extends Model {
    // ...
    protected $dates = [ /*...*/, 'last_processed'];
    // ...
}

// Config
'timezone' => 'America/Phoenix',

// View
@if ($example->last_processed)
    {{ $example->last_processed }}
@else
    Hasn't been processed!
@endif

//  Outputs  "-0001-11-30 00:00:00" when it should output "Hasn't been processed!"
0 likes
4 replies
bobbybouwmann's avatar

You can set the timezone in your migration, otherwise you need to convert the date before you access it to the correct timezone

$table->timestampTz('last_processed');

You can then define an accessor in your model and set the correct timestamp;

public function getLastProcessed($value)
{
    return $value->setTimezone('UTC'); // Or whatever time zone
}

Not sure if that last step is needed, if you set your global timezone to your timezone

stevenjeffries's avatar

Thanks for the quick response!

Let's say I change my code to that, then I have an option for the user to change their timezone (overrides the app.config value) on the front end. How would I check for null values without knowing in advance which timezone the dates will be set in?

bobbybouwmann's avatar

You can create a function on the model and pass a timezone to it I guess. Then you are save

stevenjeffries's avatar
Level 1

To anyone that may happen to google upon this, I've opened an issue, and I've come up with a temporary workaround.

You can get access to the original data before it's been sent through any mutators by calling getOriginal($field_name). The timestamp values are still not null, but they haven't yet been adjusted for timezones.

Here's my workaround:


/**
 * @param $model \Illuminate\Database\Eloquent\Model The Model instance to check.
 * @param $field string The name of the field to check against.
 * @return bool True if the date has been set, false if not.
 */
function has_date($model, $field) {
    return $model->getOriginal($field) && ($model->getOriginal($field) !== '0000-00-00 00:00:00');
}

Usage:

@if(has_date($example, 'last_processed'))
    The model has this attribute set!
@else
    The model does not have this attribute set.
@endif

Please or to participate in this conversation.