在Laravel 5.2控制器中使用PHP Carbon时,变量似乎彼此绑定.所以改变一个会影响其他人;
PHP功能:
$now = Carbon::now();
var_dump($now);
$from = $now;
$from->startOfYear();
var_dump('-----------------------------------');
var_dump($now, $from);
Run Code Online (Sandbox Code Playgroud)
结果是:
object(Carbon\Carbon)[225]
public 'date' => string '2016-02-13 21:55:36.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
string '-----------------------------------' (length=35)
object(Carbon\Carbon)[225]
public 'date' => string '2016-01-01 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
object(Carbon\Carbon)[225]
public 'date' => string '2016-01-01 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
Run Code Online (Sandbox Code Playgroud)
设置$from到年初也受到影响$now …
我需要显示三个日历,一个用于当前月份,另外两个用于接下来的两个月。
我正在使用 Carbon 进行这些计算。
今天是10月31日。
如果我写以下
$carbon = Carbon::now('UTC'); // current datetime in UTC is 8:54 AM October 31, 2016
echo $carbon->format('F') . '<br>';
echo $carbon->addMonths(1)->format('F');
Run Code Online (Sandbox Code Playgroud)
我得到这个输出
十月
十二月
我完全错过了 11 月……所以我如何在 10 月加上一个月才能得到 11 月。
有没有办法让我在Carbon中两个日期之间获得一个随机日期?例如,我尝试获取从现在到55分钟之前的一个随机日期。
$dateNow = Carbon::now();
$date25MinsAgo = Carbon::now()->subMinutes(55);
Run Code Online (Sandbox Code Playgroud)
但是,我仍然停留在这一点上。我在php上找到了一些信息,但是我想使用“ now”,因为它是播种机。我该怎么办?
我有一个数据库中的这个集合,我有一个名为"event_date"的列.
我想要的只是获取集合中该列的日期名称.
我知道你可以使用Carbon来获取名称的日期,例如,一个名为的方法->format(),但是我得到一个错误,说这个方法不存在.
我的代码到目前为止如下:
$collection = MyModel::all();
Run Code Online (Sandbox Code Playgroud)
里面有"event_date"属性或列.从那以后,我想得到将它们放入数组或集合中的日期名称,并最终计算那些天数.
为了实现这一点,我尝试了以下方法:
我尝试了->pluck()如下方法:
$filtered = collect([
'myDates'=>$collection->pluck('event_date'),
]);
Run Code Online (Sandbox Code Playgroud)
而dd($filtered)看起来像如下:
Collection {#209 ?
#items: array:1 [?
"myDates" => Collection {#243 ?
#items: array:30 [?
0 => Carbon {#244 ?
+"date": "2017-02-05 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
1 => Carbon {#218 ?
+"date": "2017-01-15 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
2 => Carbon {#250 ?
+"date": "2016-09-25 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Mexico_City"
}
3 => Carbon {#249 ? …Run Code Online (Sandbox Code Playgroud) 在PHP中检查这个自解释代码:
现实:
$dateTime = Carbon::createFromDateTime(2017, 2, 23);
echo $dateTime; // 2017-02-23 00:00:00
echo $dateTime->startOfYear(); // 2017-12-31 23:59:59
echo $dateTime; // 2017-12-31 23:59:59
Run Code Online (Sandbox Code Playgroud)
请注意,4号线,价值$dateTime为2017-12-31 23:59:59.那是因为在第3行.
但为什么?我知道Carbon的startOfYear()是一个修饰符,但是我们怎么能在不修改自己的情况下获得一年的开始日期
预期:
$dateTime = Carbon::createFromDateTime(2017, 2, 23);
echo $dateTime; // 2017-02-23 00:00:00
echo $dateTime->startOfYear(); // 2017-12-31 23:59:59
echo $dateTime; // 2017-02-23 00:00:00
Run Code Online (Sandbox Code Playgroud)
在上面,注意第4行.实际上,第4行输出2017-12-31 23:59:59.
我正在使用以下代码:
$birthday = request('birthday_day') . "-" . request('birthday_month') . "-" . request('birthday_year');
$birthday = Carbon::createFromFormat('d-m-Y', $birthday);
$min_year = date('Y') - 10;
if ($birthday->gte(Carbon::create($min_year, 1, 1))) {
return back()->withErrors(['message' => 'You need to be at least 10 years old to sign up.'])->withInput();
}
$user = User::create([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
'password' => bcrypt(request('password')),
'birthday' => $birthday, //THE PROBLEM
'gender' => request('gender'),
'verified' => 0,
'verification_code' => bcrypt(str_random(30) . request('email'))
]);
Run Code Online (Sandbox Code Playgroud)
我在运行时收到以下错误消息:
SQLSTATE[22007]: Invalid datetime format: 1292 …Run Code Online (Sandbox Code Playgroud) 我在理解 Laravel 如何处理日期和碳时遇到了麻烦。我有一个模型,它有一个名为published_at 的特殊字段(它是da DATE 字段)。当我保存对象的实例时,我希望 Laravel 使用 carbon 转换该字段。
这是我的模型:
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Addon extends Model
{
// ...
protected $dates = [
'published_at'
];
// ....
/**
* Set the published_at date format
*
* @param $date
* @return string
*/
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('d/m/Y', $date);
}
// ....
}
Run Code Online (Sandbox Code Playgroud)
因此,据我所知,published_at 字段现在应该使用 d/m/Y 格式保存为碳的实例。但是,它在数据库中保存为 mm-dd-yyyy。
最后,我不在乎它是如何保存到数据库中的,所以这就是为什么我想创建一个getPublishedAtAttribute并以我想要的格式检索它。那也没有用。
我想要的只是 Laravel 将其格式化为 d/m/Y。没有我必须做的事情,比如{{ $addon->published_at->format('d/m/Y') }}每当我展示它时。
顺便说一句:我想同样的事情created_at和updated_at …
我的应用程式从前端开始有这个日期:
'13 -07-2017 14:00'
我会测试:
try{
Carbon::createFromFormat('d-m-Y H:i', $date);
}catch (\Exception $err){
return false;
}
Run Code Online (Sandbox Code Playgroud)
为什么总是返回此错误?
InvalidArgumentException:小时不能大于12
在Carbon.php(第582行)中的Carbon :: createFromFormat('dmY h:i','13 -07-2017 14:13')
所以我使用CAPITAL H,但是我的异常抛出了'h'
我正在使用Laravel框架并使用Carbon包进行日期转换
我无法将日期格式转换为mysql格式.我有以下代码
$request->event_start_date 会的 25/08/2017
print_r(carbon::parse($request->event_start_date));
Run Code Online (Sandbox Code Playgroud)
当$request->event_start_date被03/08/2017那么它将打印成
Carbon\Carbon Object( [date] => 2017-03-08 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC)
Run Code Online (Sandbox Code Playgroud)
但是,如果日期是25/08/2017那么它将抛出erorr as
"G:\ XAMPP\htdocs\myproject\vendor \nesbot\carbon\src\Carbon\Carbon.php"行:291消息:"DateTime :: __ construct():无法解析时间字符串(25/08/2017)at位置0(2):意外字符"
需要转换25/08/2017为Mysql日期格式.我已经尝试了很多来解决这个问题.最后发布在这里,以便我从你那里得到一些帮助
谢谢
我正在尝试提取仅7天的记录,而不是更早或更早的记录。但它不起作用,我正在使用Carbon。
->where(DB::raw('date(AppDate)'), Carbon::now()->subDays(7))
Run Code Online (Sandbox Code Playgroud) php ×10
php-carbon ×10
laravel ×9
laravel-5 ×4
datetime ×3
date ×2
laravel-5.2 ×1
mysql ×1
time ×1