PHP code example of jlorente / laravel-eloquent-splitted-dates-trait

1. Go to this page and download the library: Download jlorente/laravel-eloquent-splitted-dates-trait 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/ */

    

jlorente / laravel-eloquent-splitted-dates-trait example snippets




use Illuminate\Database\Eloquent\Model;
use Jlorente\Laravel\Eloquent\Concerns\SplittedDates\HasSplittedDates;

class Subscription extends Model
{
    use HasSplittedDates;

    /**
     * The splitted dates attributes.
     *
     * @var array
     */
    protected $splittedDates = [
        'begin_at'
    ];
}



$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2);

echo $subscription->begin_at_year; // 2019
echo $subscription->begin_at_month; // 10
echo $subscription->begin_at_day; // 2



$subscription = new Subscription();
$subscription->begin_at_year = 2025 // 2025

echo $subscription->begin_at->toDateTimeString(); // 2025-01-01 00:00:00

$subscription->begin_at_day = 26 // 2025

echo $subscription->begin_at->toDateTimeString(); // 2025-01-26 00:00:00



$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2); // OK
$subscription->begin_at = '2019-10-02' // OK
$subscription->begin_at = '2019-10-02 22:11:45' // OK
$subscription->begin_at = strtotime() // OK




use Illuminate\Database\Eloquent\Model;
use Jlorente\Laravel\Eloquent\Concerns\SplittedDates\HasSplittedDates;

class Subscription extends Model
{
    use HasSplittedDates;

    /**
     * The splitted dates attributes.
     *
     * @var array
     */
    protected $splittedDates = [
        'begin_at' => ['hour', 'minute']
    ];
}



$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2, 15, 45, 12);

echo $subscription->begin_at_hour; // 15
echo $subscription->begin_at_minute; // 45

$subscription->begin_at_minute = 31;

echo $subscription->begin_at->toDateTimeString(); // 2019-10-02 15:31:12



$subscription = new Subscription();
$subscription->begin_at = Carbon::create(2019, 10, 2, 15, 45, 12);

$subscription->begin_at_hour = 27; // => Invalid value

echo $subscription->begin_at_hour; // 27
echo $subscription->begin_at->toDateTimeString(); // 2019-10-03 03:45:12