Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tobya/laravel-mssql-dateformat",
"description": "Check and set MSSQL universal date format used by Laravel to 'Ymd' to ensure universal compatibility",
"description": "Utilise an alternative Inherited MSSQL Grammar that sets universal date format",
"type": "laravel-package",
"license": "MIT",
"autoload": {
Expand All @@ -15,7 +15,7 @@
}
],
"require": {
"laravel/framework": "^8.0|^9.0|^10.0|^11.0"
"laravel/framework": "^8.0|^9.0|^10.0|^11.0|^12.0"
},
"extra": {
"laravel": {
Expand Down
31 changes: 3 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ There is an [international date format (ISO standard)](https://www.iso.org/iso-8
and is NOT universal and incorrectly interprets `Y-m-d` as `Y-d-m` which is beyond idiotic.
Laravel uses `Y-m-d` as their international format, which can lead to errors depending on SQL SERVER Settings.

There is one format `Ymd` which is absolutely gauranteed to always be interpreted by SQLServer as `Ymd`. This command patches the file in
illuminate library to use `Ymd` instead of `Y-m-d`. I [Requested](https://github.com/laravel/framework/issues/49074) that the change be made in the illuminate library but it was felt the change
There is one format `Ymd` which is absolutely gauranteed to always be interpreted by SQLServer as `Ymd`.
This command extends the SqlServer grammar to use `Ymd` instead of `Y-m-d`. I [Requested](https://github.com/laravel/framework/issues/49074) that the change be made in the illuminate library but it was felt the change
was too big to be made.

I hope this is helpful to those of you out there using MSSQL with PHP/Laravel.
Expand All @@ -19,33 +19,8 @@ composer require tobya/laravel-mssql-dateformat
````
### To Run

Run by calling the larvel command
No need to run, it is automatically pulled in via Provider.

````dotenv
artisan mssql:check-universal-date --update
````
You can run without `--update` to do the check without patching the file.

## Configuration

It is suggested that you add the following to your project `composer.json` file so this command is automatically run
on install and update.

````dotenv

"scripts": {
"post-update-cmd": [
"@php artisan mssql:check-universal-date --update"
],
"post-install-cmd": [
"@php artisan mssql:check-universal-date --update"
]
}
````
This is due to the fact that whenever `composer update` or `composer install` is run and the illuminate package
is updated it will overwrite the `SqlServerGrammar.php` with the origional version, so it is necessary to call
the command whenever this has the potential of happening. If no change has been made to the file it will
not be modified.

#### Further reading on why this is necessary

Expand Down
36 changes: 3 additions & 33 deletions src/Console/CheckSQLGrammerDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CheckSQLGrammerDate extends Command
*
* @var string
*/
protected $description = 'Checks if the illuminate files in vendor directory are using incorrect date format. ';
protected $description = 'Deprecated: Date Format is now updated by use of inherited grammar. This command is no longer required. ';

/**
* Create a new command instance.
Expand All @@ -63,40 +63,10 @@ public function __construct()
*/
public function handle()
{
$filetocheck = base_path('vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\SqlServerGrammar.php');
if (file_exists($filetocheck)){
$file_txt = Str::of(file_get_contents($filetocheck));
$datestr = 'return \'Y-m-d H:i:s.v\';';
if ($file_txt->contains($datestr)){
if ($this->option('update')){
$UpdatedFile_txt = $file_txt->replace($datestr, 'return \'Ymd H:i:s.v\';');
file_put_contents($filetocheck,$UpdatedFile_txt);

$this->comment("
**********************************
Incorrect Date Format value found
**********************************
File on disk: $filetocheck");
$this->info('
------------------
Updated
------------------');
return 0;
} else {
$this->warn( $this->NoUpdateMessage($filetocheck));
$this->warn('Not Updated');
return 0;
}

}
}
$this->info( "Date Format Appears to be ok.");
$this->warn( "Deprecated: Date Format is now updated by use of inherited grammar. This command is no longer required. ");
return 0;
}

public function NoUpdateMessage($filetocheck){
return "
$filetocheck
has been overwritten with the wrong date format. mssql:CheckSQLDate --update to update the file. ";
}

}
21 changes: 21 additions & 0 deletions src/Grammars/MSSQLGrammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tobya\MSSQLDateformat\Grammars;

use Illuminate\Database\Query\Grammars\SqlServerGrammar;

class MSSQLGrammar extends SqlServerGrammar
{

/**
* Get the format for database stored dates.
* Override to use universal MSSQL format
*
* @return string
*/
public function getDateFormat()
{
// return 'Y-m-d H:i:s.v';
return 'Ymd H:i:s.v';
}
}
5 changes: 4 additions & 1 deletion src/Providers/MSSQLUniversalDateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Tobya\MSSQLDateformat\Console\CheckSQLGrammerDate;
use Illuminate\Support\Facades\DB;

class MSSQLUniversalDateProvider extends \Illuminate\Support\ServiceProvider
{
Expand All @@ -28,5 +29,7 @@ public function boot()
$this->commands([
CheckSQLGrammerDate::class
]);

DB::connection()->setQueryGrammar(new \Tobya\MSSQLDateformat\Grammars\MSSQLGrammar(DB::connection()));
}
}
}