PHP code example of pdatepicker / persian-datepicker
1. Go to this page and download the library: Download pdatepicker/persian-datepicker 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/ */
pdatepicker / persian-datepicker example snippets
use PDatepicker\Rules\JalaliDate;
public function store(Request $request)
{
$request->validate([
'birth_date' => ['
use PDatepicker\Rules\JalaliDate;
class UserRequest extends FormRequest
{
public function rules()
{
return [
'birth_date' => ['
// In your model
use PDatepicker\Facades\PDatepicker;
class User extends Model
{
/**
* Get the birth date in Jalali format.
*
* @return string
*/
public function getBirthDateJalaliAttribute()
{
if (!$this->birth_date) return null;
return PDatepicker::toJalali($this->birth_date, 'Y/m/d');
}
/**
* Set the birth date from Jalali to Gregorian format.
*
* @param string $value
* @return void
*/
public function setBirthDateJalaliAttribute($value)
{
if (!$value) {
$this->attributes['birth_date'] = null;
return;
}
$this->attributes['birth_date'] = PDatepicker::toGregorian($value, 'Y-m-d');
}
}
return [
// Default date format
'format' => 'YYYY/MM/DD',
// Visual theme (default, dark, blue)
'theme' => 'default',
// Enable RTL support for Persian text
'rtl' => true,
// Default language (fa, en)
'language' => 'fa',
// Auto close picker after selection
'autoClose' => true,
// Enable time picker component
'timePicker' => false,
// Time format (HH:mm:ss, HH:mm, hh:mm A)
'timeFormat' => 'HH:mm:ss',
// Calendar type (date, month, year)
'type' => 'date',
// Initial view mode
'viewMode' => 'day',
];
use PDatepicker\Facades\PDatepicker;
// Convert Gregorian to Jalali
$jalaliDate = PDatepicker::toJalali('2023-03-21');
// Convert Jalali to Gregorian
$gregorianDate = PDatepicker::toGregorian('1402/01/01');
// Get current Jalali date
$now = PDatepicker::now();
// Format for database insertion
$formattedDate = PDatepicker::toGregorian($request->birth_date, 'Y-m-d');