当前位置: 首页>>代码示例>>PHP>>正文


PHP Carbon::instance方法代码示例

本文整理汇总了PHP中Carbon\Carbon::instance方法的典型用法代码示例。如果您正苦于以下问题:PHP Carbon::instance方法的具体用法?PHP Carbon::instance怎么用?PHP Carbon::instance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Carbon\Carbon的用法示例。


在下文中一共展示了Carbon::instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: testInstanceFromDateTimeKeepsMicros

 public function testInstanceFromDateTimeKeepsMicros()
 {
     $micro = 254687;
     $datetime = \DateTime::createFromFormat('Y-m-d H:i:s.u', '2014-02-01 03:45:27.' . $micro);
     $carbon = Carbon::instance($datetime);
     $this->assertSame($micro, $carbon->micro);
 }
开发者ID:uwmadisoncals,项目名称:Cluster-Plugins,代码行数:7,代码来源:InstanceTest.php

示例2: asDateTime

 /**
  * Return a timestamp as DateTime object.
  *
  * @param  mixed  $value
  * @return \Carbon\Carbon
  */
 protected function asDateTime($value)
 {
     // \Log::debug('asDateTime: '.$value);
     // If this value is an integer, we will assume it is a UNIX timestamp's value
     // and format a Carbon object from this timestamp. This allows flexibility
     // when defining your date fields as they might be UNIX timestamps here.
     if (is_numeric($value)) {
         // \Log::debug('a');
         return Carbon::createFromTimestamp($value);
     } elseif (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})$/', $value)) {
         // \Log::debug('b');
         return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
     } elseif (preg_match('/^(\\d{2}):(\\d{2})(:(\\d{2}))?$/', $value)) {
         // \Log::debug('c');
         $value = Carbon::createFromFormat('H:i:s', $value);
     } elseif (!$value instanceof DateTime) {
         // \Log::debug('d');
         if (null === $value) {
             return $value;
         }
         $format = $this->getDateFormat();
         return Carbon::createFromFormat($format, $value);
     }
     return Carbon::instance($value);
 }
开发者ID:npmweb,项目名称:laravel-base,代码行数:31,代码来源:BaseModel.php

示例3: __toCarbon

 /**
  * Return a timestamp as Carbon object.
  *
  * A slightly modified version of \Illuminate\Database\Eloquent\Model::asDateTime().
  *
  * @param mixed $value
  * @param string|null $format
  * @return Carbon
  */
 protected function __toCarbon($value, $format = null)
 {
     // If this value is already a Carbon instance, we shall just return it as is.
     // This prevents us having to reinstantiate a Carbon instance when we know
     // it already is one, which wouldn't be fulfilled by the DateTime check.
     if ($value instanceof Carbon) {
         return $value;
     }
     // If the value is already a DateTime instance, we will just skip the rest of
     // these checks since they will be a waste of time, and hinder performance
     // when checking the field. We will just return the DateTime right away.
     if ($value instanceof DateTime) {
         return Carbon::instance($value);
     }
     // If this value is an integer, we will assume it is a UNIX timestamp's value
     // and format a Carbon object from this timestamp. This allows flexibility
     // when defining your date fields as they might be UNIX timestamps here.
     if (is_numeric($value)) {
         return Carbon::createFromTimestamp($value);
     }
     // If the value is in simply year, month, day format, we will instantiate the
     // Carbon instances from that format. Again, this provides for simple date
     // fields on the database, while still supporting Carbonized conversion.
     if (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})$/', $value)) {
         return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
     }
     // Use the $format if one was given.
     if (isset($format) && is_string($format)) {
         return Carbon::createFromFormat($format, $value);
     }
     // Finally, we'll let carbon work out the format (watch out for ambiguous days/months).
     return Carbon::parse($value);
 }
开发者ID:artissant,项目名称:laravel,代码行数:42,代码来源:Carbonize.php

示例4: convertToPHPValue

 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return $value;
     }
     return Carbon::instance(parent::convertToPHPValue($value, $platform));
 }
开发者ID:atiarda,项目名称:bolt,代码行数:10,代码来源:CarbonDateTimeType.php

示例5: getDate

 /**
  * @return Carbon
  */
 public function getDate()
 {
     if ($this->date) {
         return Carbon::instance($this->date);
     }
     return null;
 }
开发者ID:ymnl007,项目名称:Usher,代码行数:10,代码来源:Date.php

示例6: dateInfo

 public function dateInfo($day, $month, $year)
 {
     $this->day = $day;
     $this->month = $month;
     $this->year = $year;
     $yearnow = date("Y");
     $nowday = Carbon::instance(new \DateTime());
     if (!$this->validDate()) {
         $mes = 'An incorrect date format';
         return $mes;
     }
     $newyear = Carbon::instance(new \DateTime("01.01.{$yearnow}"));
     $enterdate = Carbon::instance(new \DateTime("{$this->day}.{$this->month}.{$this->year}"));
     $diffday = $enterdate->day - $nowday->day;
     $diffmonth = $enterdate->month - $nowday->month;
     $diffyear = $enterdate->year - $nowday->year;
     $daystartyear = $nowday->dayOfYear;
     $monthstartyear = $nowday->month - $newyear->month;
     $timezons = $nowday->timezoneName;
     $mes = 'You entered ' . $enterdate->toDateString() . '<br/>';
     $mes .= 'Now ' . $nowday->toDateString() . '<br/>';
     if ($diffday == 0 && $diffmonth == 0 && $diffyear == 0) {
         $mes .= 'You enter the current date';
     } elseif ($diffday > 0 || $diffmonth > 0 || $diffyear > 0) {
         $mes .= 'More than the current date entered in the ' . abs($diffday) . ' days and ' . abs($diffmonth) . ' month ' . abs($diffyear) . ' year(s)';
     } else {
         $mes .= 'Less than the current date entered by ' . abs($diffday) . ' days and ' . abs($diffmonth) . ' month ' . abs($diffyear) . ' year(s)';
     }
     $mes .= "<br/> Your time zone: {$timezons}";
     $mes .= "<br/> Since the beginning of this year went {$monthstartyear} full months or {$daystartyear} days";
     return $mes;
 }
开发者ID:alexgoncharcherkassy,项目名称:Lesson2PHP,代码行数:32,代码来源:ClassDate.php

示例7: __construct

 /**
  * Create a new CarbonPeriod instance.
  *
  * @param \DateTime $startDate
  * @param \DateTime $endDate
  */
 public function __construct(\DateTime $startDate = null, \DateTime $endDate = null)
 {
     $this->startDate = new CarbonDate($startDate);
     $this->endDate = $endDate ? CarbonDate::instance($endDate) : $this->startDate->copy()->addDay()->startOfDay();
     $this->order();
     return $this;
 }
开发者ID:asdf20122012,项目名称:carbon-period,代码行数:13,代码来源:CarbonPeriod.php

示例8: run

 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker\Factory::create();
     // Create a faker, add en_SG providers
     $faker->addProvider(new Faker\Provider\en_SG\Address($faker));
     $faker->addProvider(new Faker\Provider\en_SG\Enhanced($faker));
     $faker->addProvider(new Faker\Provider\en_SG\Person($faker));
     $faker->addProvider(new Faker\Provider\en_SG\PhoneNumber($faker));
     $faker->seed(9876);
     // Calling the same script twice with the same seed produces the same results
     $durations = [30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180];
     // Insert 30 dummy records
     foreach (range(1, 30) as $index) {
         $startDate = $faker->dateTimeBetween('-3 months', '3 months');
         $startHour = ['08', '09', '10', '11', '13', '14', '15', '16'];
         $startMin = ['00', '15', '30', '45'];
         while (Carbon::instance($startDate)->dayOfWeek === Carbon::SUNDAY) {
             $startDate = $faker->dateTimeBetween('-3 months', '3 months');
         }
         if ($startDate === Carbon::SATURDAY) {
             $startHour = ['08', '09', '10', '11'];
         }
         $startTime = $faker->randomElement($startHour) . ":" . $faker->randomElement($startMin) . ":00";
         $elderly = Elderly::with('centre')->find($faker->numberBetween(1, 15));
         $staffList = $elderly->centre->staff->lists('staff_id')->toArray();
         Activity::create(['datetime_start' => $startDate->format('Y-m-d') . " " . $startTime, 'expected_duration_minutes' => $faker->randomElement($durations), 'category' => 'transport', 'more_information' => $faker->optional(0.4, '')->sentence(10, true), 'location_from_id' => $elderly->centre_id, 'location_to_id' => $faker->numberBetween(4, 6), 'elderly_id' => $elderly->elderly_id, 'centre_id' => $elderly->centre_id, 'staff_id' => $faker->randomElement($staffList)]);
     }
 }
开发者ID:jonnylow,项目名称:CrowdSourcingApplicationWeb,代码行数:33,代码来源:ActivitiesTableSeeder.php

示例9: testConstructorShouldAcceptDateTimeObject

 public function testConstructorShouldAcceptDateTimeObject()
 {
     $dt = new DateTime("2014-01-09 11:14:15.638276");
     $carbon = Carbon::instance($dt);
     $test = new DateRange($dt);
     $this->assertEquals($carbon, $test->start);
 }
开发者ID:viniciusferreira,项目名称:daily,代码行数:7,代码来源:DateRangeTest.php

示例10: handle

 /**
  * @param CreateWorkshop $command
  * @throws Exception
  */
 public function handle(CreateWorkshop $command)
 {
     if (!is_array($command->lessons) || count($command->lessons) === 0) {
         throw new Exception("Lessons collection can't be empty!");
     }
     $calendar = new Calendar(null, $command->title);
     $command->calendar = $calendar;
     $workshop = $this->workshopFactory->createFromCommand($command);
     /** @var CreateLesson $lessonCommand */
     foreach ($command->lessons as $lessonCommand) {
         $duration = Carbon::instance($lessonCommand->startDate)->diffInMinutes(Carbon::instance($lessonCommand->endDate), true);
         $eventCommand = new CreateEventCommand();
         $eventCommand->calendar = $calendar;
         $eventCommand->duration = $duration;
         $eventCommand->startDate = $lessonCommand->startDate;
         $eventCommand->endDate = Carbon::instance($lessonCommand->startDate)->addMinutes($duration);
         $eventCommand->repetitionDays = [];
         $eventCommand->type = EventType::TYPE_SINGLE;
         $event = $this->eventFactory->createFromCommand($eventCommand);
         $occurrences = $this->occurrenceFactory->generateCollectionFromEvent($event);
         $calendar->events()->add($event);
         if (count($occurrences) == !1) {
             throw new Exception('Could not generate occurrences from event');
         }
         $event->setOccurrences($occurrences);
         $lessonCommand->workshop = $workshop;
         $lessonCommand->event = $event;
         $lesson = $this->lessonFactory->createFromCommand($lessonCommand);
         $workshop->addLesson($lesson);
     }
     $this->calendarRepository->insert($calendar);
     $this->workshopRepository->insert($workshop);
 }
开发者ID:uirapuru,项目名称:evento,代码行数:37,代码来源:CreateWorkshopHandler.php

示例11: convertToPHPValue

 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     $result = parent::convertToPHPValue($value, $platform);
     if ($result instanceof \DateTime) {
         return Carbon::instance($result);
     }
     return $result;
 }
开发者ID:digvijaymohite,项目名称:e-tender,代码行数:8,代码来源:CarbonDateTimeTzType.php

示例12: asDateTime

 /**
  * Ensure Timestamps are returned in DateTime.
  *
  * @param \DateTime $value
  *
  * @return \DateTime
  */
 protected function asDateTime($value)
 {
     // Legacy support for Laravel 5.0
     if (!$value instanceof Carbon) {
         return Carbon::instance($value);
     }
     return parent::asDateTime($value);
 }
开发者ID:duxet,项目名称:laravel-rethinkdb,代码行数:15,代码来源:Model.php

示例13: onGracePeriod

 /**
  * Determine if the entity is on grace period after cancellation.
  *
  * @return bool
  */
 public function onGracePeriod()
 {
     if (!is_null($endsAt = $this->billing_subscription_ends_at)) {
         return Carbon::now()->lt(Carbon::instance($endsAt));
     } else {
         return false;
     }
 }
开发者ID:rained23,项目名称:laravel-billing,代码行数:13,代码来源:SubscriptionBillableTrait.php

示例14: setUp

 public function setUp()
 {
     // Setup ExampleModel.
     $this->reflect(new ExampleModel());
     // Setup Carbon instance.
     $this->dateTime = new DateTime();
     $this->carbon = Carbon::instance($this->dateTime);
 }
开发者ID:thisvessel,项目名称:carbonated,代码行数:8,代码来源:ConversionTest.php

示例15: next_lesson_of

 public function next_lesson_of($name, $date = false)
 {
     $date = $date ? Carbon::instance($date) : Carbon::now();
     foreach ($this->lessons()->where('start', '>=', $date->setTime(0, 0, 0)->format("Y-m-d H:i:s"))->where('end', '<=', $date->setTime(23, 59, 59)->format("Y-m-d H:i:s"))->get() as $lesson) {
         if ($lesson->subject == $name) {
             return $lesson;
         }
     }
 }
开发者ID:oeed,项目名称:Keystone-Next,代码行数:9,代码来源:User.php


注:本文中的Carbon\Carbon::instance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。