PHP code example of fereydooni / unixtime

1. Go to this page and download the library: Download fereydooni/unixtime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fereydooni / unixtime example snippets


    use Fereydooni\Unixtime\HasTimestampEquivalents;

    class Customer extends Model
    {
        use HasTimestampEquivalents;
    }
    

    $customer = Customer::find(1);
    echo $customer->created_at_unix;        // 1731341349
    echo $customer->last_order_date_unix;   // Also managed automatically
    

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Schema::create('orders', function (Blueprint $table) {
    $table->timestamp('confirmed_at')->nullable();
    $table->timestamps();
    $table->softDeletes();

    // Adds confirmed_at_unix, created_at_unix, updated_at_unix, deleted_at_unix
    $table->timestampEquivalents();
});

$table->timestamp('verified_at')->nullable();
$table->timestampEquivalent('verified_at'); // Adds verified_at_unix (nullable, indexed)

$table->dropTimestampEquivalents(['verified_at', 'created_at']);

class Customer extends Model
{
    use HasTimestampEquivalents;

    // Only manage these columns (otherwise they are auto-discovered from the DB)
    protected $timestampEquivalentColumns = ['created_at', 'last_order_date'];

    // Skip columns even if they look like datetimes
    protected $excludedTimestampColumns = ['date_of_birth'];

    // Customize suffix (default: '_unix')
    protected $timestampColumnSuffix = '_timestamp';
}

$customer->getUnixTimestamp('created_at'); // => 1731341349
$customer->hasTimestampColumn('created_at_unix'); // true/false

// Uses created_at_unix behind the scenes
Customer::latest()->take(50)->get();

// orderByDesc('shipped_at') becomes shipped_at_unix when available
Order::orderByDesc('shipped_at')->paginate();
bash
php artisan timestamp-equivalents:sync
bash
php artisan timestamp-equivalents:sync --model="App\\Models\\Customer" --backfill
bash
php artisan test --filter=HasTimestampEquivalentsTest