我的数据库中有消息。并且在消息中有发送日期。我有消息模型。
<?php
namespace App;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Message extends Model
{
protected $fillable = ['id', 'subject', 'message', 'from', 'to', 'created_at'];
public function sender() {
return $this->belongsTo(User::class, 'from');
}
public function receiver() {
return $this->belongsTo(User::class, 'to');
}
public function getCreatedAtAttribute($date)
{
return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('M d H:i');
}
}
如何以日期格式H:i
显示今天发送的消息。以及如何以日期格式M-d
显示今年发送的所有消息(没有今天发送的消息)。以及如何以日期格式Y-m-d
显示过去几年的已发送消息。我必须在哪里编写消息检查日期的代码并返回所需的格式?
这里是获取日期差异的代码:
echo $today = date('Y-m-d H:i:s');
echo $message_date = "2018-01-07 01:34:35"; // Work date('H:i')
// $message_date = "2017-01-07 01:34:35"; // Work date('Y-m-d')
// $message_date = "2017-12-07 01:34:35"; // Work date('M d')
$date_diff = date_diff(date_create($today), date_create($message_date));
if($date_diff->days == 0) echo '<p>'.date('H:i').'</p>';
if($date_diff->y == 0 AND $date_diff->m > 0) echo '<p>'.date('M d').'</p>';
if($date_diff->y > 0) echo '<p>'.date('Y-m-d').'</p>';
我在Laravel中编写了简单的代码,但它不起作用返回"Not working!"
public function getCreatedAtAttribute($date)
{
$today = date_create(date('Y-m-d H:i:s'));
$message_date = date_create($date);
$date_diff = date_diff($today, $message_date);
if($date_diff->days == 0) return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('H:i');
if($date_diff->y == 0 AND $date_diff->m > 0) return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('M d');
if($date_diff->y > 0) return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
return "Not working!";
}
答案 0 :(得分:4)
首先,您不应该为H:i
日期设置访问者,以将其转换为字符串。删除访问者或重命名它。
如何以日期格式
Message::whereDate('created_at', Carbon::today())->get();
显示今天发送的邮件。
获取消息:
Message::whereBetween('created_at', [now()->startOfYear(), Carbon::yesterday()->endOfDay()])->get();
以及如何以日期格式M-d显示今年发送的所有消息(没有今天发送的消息)
Message::where('created_at', '<', now()->startOfYear())->get();
如何以日期格式Y-m-d显示过去几年的发送消息。
format()
要在Blade模板中显示消息,请使用@foreach ($messages as $message)
{{ $message->created_at->format('H:i') }}
@endforeach
,例如:
C:\Users\ahkim>pip install pymssql
Collecting pymssql
Downloading pymssql-2.1.3.tar.gz (897kB)
100% |████████████████████████████████| 901kB 1.9MB/s
Building wheels for collected packages: pymssql
Running setup.py bdist_wheel for pymssql ... error
Complete output from command C:\Users\ahkim\AppData\Local\Continuum\anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ahkim\\AppData\\Local\\Temp\\pip-build-mfok55b5\\pymssql\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d C:\Users\ahkim\AppData\Local\Temp\tmp127kpd9epip-wheel- --python-tag cp36:
setup.py: platform.system() => 'Windows'
setup.py: platform.architecture() => ('64bit', 'WindowsPE')
running bdist_wheel
running build
running build_ext
building '_mssql' extension
error: [WinError 3] The system cannot find the path specified: 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\PlatformSDK\\lib'
----------------------------------------
Failed building wheel for pymssql
Running setup.py clean for pymssql
Failed to build pymssql
Installing collected packages: pymssql
Running setup.py install for pymssql ... error
Complete output from command C:\Users\ahkim\AppData\Local\Continuum\anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ahkim\\AppData\\Local\\Temp\\pip-build-mfok55b5\\pymssql\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\ahkim\AppData\Local\Temp\pip-r247wrkl-record\install-record.txt --single-version-externally-managed --compile:
setup.py: platform.system() => 'Windows'
setup.py: platform.architecture() => ('64bit', 'WindowsPE')
running install
running build
running build_ext
cythoning _mssql.pyx to _mssql.c
warning: _mssql.pyx:143:4: Exception already a builtin Cython type
building '_mssql' extension
error: [WinError 3] The system cannot find the path specified: 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\PlatformSDK\\lib'
----------------------------------------
Command "C:\Users\ahkim\AppData\Local\Continuum\anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ahkim\\AppData\\Local\\Temp\\pip-build-mfok55b5\\pymssql\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\ahkim\AppData\Local\Temp\pip-r247wrkl-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\ahkim\AppData\Local\Temp\pip-build-mfok55b5\pymssql\