i want to calculate the time difference and insert it into database...my model name is "booking" and the start time and end time will be taken input from users..and total_duration will be calculated from these two and will be inserted into database...i use these codes...but won't working.this is my controller.
<?php
/*namespace App\booking;*/
use Carbon\Carbon;
namespace App\Http\Controllers;
use App\booking;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
class RoombookController extends Controller
{
public function showreport(Request $request)
{
/* dd($request->all());
*/ $time= Carbon.now();
$booking = new booking;
$booking->bookdate = $request->input('bookdate');
$booking->roomname = $request->input('roomname');
//echo $datefrom;
$booking->starttime =$startTime= $request->input('starttime');
$booking->endtime = $finishTime=$request->input('endtime');
$booking->purpose = $request->input('Purpose');
//echo $dateto;
$time->sTime = Carbon::parse($startTime);
$time->fTime = Carbon::parse($finishTime);
$time->total_time = $fTime->diffForHumans($sTime);
$booking->total_duration = $time->total_time;
$booking->save();
}
}
Using Carbon for calculation execution time is not very good idea. Just use plain old microtime():
$start = microtime(true);
.... // Do something here.
$end = microtime(true);
$time = $end - $start;
I think you should first convert it into seconds
$totalDuration = $finishTime->diffInSeconds($startTime);
and then desirable format
gmdate('H:i:s', $totalDuration);
or try this if this work for you
$finishTime->diff($startTime)->format('%H:%i:%s');
Related
I'm working on a Symfony project that makes use of a
Repository file in which I have declared and created an instance of a query builder
(repository1)
$mk = $this->createQueryBuilder('up');
$later = date("Y-m-d", strtotime("-3 months"));
$today = date("Y-m-d");
$mk->andWhere('up.period BETWEEN :start AND :end');
$mk->setParameter('start', $later);
$mk->setParameter('end', $today);
return $mk->getQuery()->getResult();
automatically this generates data for my page between the above dates.
Now I want to create a form where I can make a search between two dates.
with the intention of passing the posted data from this form to my controller into my method below
My controller below (controller1)
protected function getData(EntityManagerInterface $entityManager,Request $request) {
// this code to get data from repository
$entityManager->getRepository(repository1::class)->getName()
// receive posted data
$date1 = $request->get('date');
$date2 = $request->get('date');
// now how to pass data to my repository1
}
Please how do I edit what I have to post data from within my controller to my (repository1)
so then it would be
$mk = $this->createQueryBuilder('up');
$later = $date1;
$today = $date2;
$mk->andWhere('up.period BETWEEN :start AND :end');
$mk->setParameter('start', $later);
$mk->setParameter('end', $today);
return $mk->getQuery()->getResult();
is this even possible, or im over thinking it?
RepositoryClass
public function getByStartEndDate(DateTimeInterface $start, DateTimeInterface $end)
{
return $this->createQueryBuilder('up')
->andWhere('up.period BETWEEN :start AND :end')
->setParameter('start', $start)
->setParameter('end', $end)
->getQuery()
->getResult()
;
}
Controller Class
private function getData(Request $request, RepositoryClass $repo)
{
// May need to convert these to DateTime objects
$start = $request->get('start');
$end = $request->get('end');
$records = $repo->getByStartEndDate($start, $end);
// do what you want with the records here
}
You can give the dates as parameters to your method.
Controller Class:
protected function getData(EntityManagerInterface $entityManager,Request $request) {
$start = $request->get('start') ? new \DateTime($request->get('start')) : null;
$end = $request->get('end') ? new \DateTime($request->get('end')) : null;
$result = $entityManager->getRepository(Entity::class)->getName($start, $end);
// do with result as desired
}
Repository Class:
public function getName(\DateTimeInterface $start, \DateTimeInterface $end)
{
return $this->createQueryBuilder('up')
->andWhere('up.period BETWEEN :start AND :end')
->setParameter('start', $start)
->setParameter('end', $end)
->getQuery()
->getResult()
;
}
I have a Leave Application that checks when the users request a leave and after approval, it removes the days from the total amount they have left in the database the problem is the leave is taken only the latest so if 2 people were requesting at the same time it would take it for both the same amount because it's only taking it from the latest which is here in the User Model
public function leaveBalance($id)
{
$leave =new Leave;
$daysUsed= $leave->numberOfDays($id);
$days_granted = User::where('id', $id)
->latest()->first();
$leave_days_granted = $days_granted->leave_days_granted;
return $leave_days_granted - $daysUsed;
}
And here in the Leave Model
protected $dates = ['from', 'to'];
public function numberOfDays($id)
{
$datesx =Leave::where('user_id', $id)
->latest()->first();
$from =$datesx->from;
$to =$datesx->to;
$datetime1 = new DateTime($from);
$datetime2 = new DateTime($to);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');//now do whatever you like with $days
return $days;
}
As you can see it's taking it from the ->latest()->first(); but I don't know another way to select it and thus why I need help I would appreciate any suggestion
User::where('id', $id)->first();
After searching documentation about 2 hours and not finding a answer (https://charts.erik.cat/), maybe someone will help me with this... I'm using Laravel Charts chart.js library. How to make that on top of my column would be displayed data labels? like in this picture on DC coulmn:
This is my code:
<?php
namespace App\Http\Controllers;
use App\Content;
use App\Charts\UserLineChart;
use Charts;
use Illuminate\Http\Request;
class ChartController extends Controller
{
public function index(Request $request)
{
$content = Content::where('vei_sn', '23333')->get();
if ($request->has('date_from') && $request->input('date_from') != '' && $request->has('date_to') && $request->input('date_to') != '') {
$datefrom = $request->input('date_from');
$dateto = $request->input('date_to');
$content = $content->whereBetween('op_date', [$datefrom, $dateto]);
}
$OBD = $content->where('con_type', 'OBD')->count();
$DC = $content->where('con_type', 'DC')->count();
$BSL = $content->where('con_type', 'BSL')->count();
$content = $content->pluck('con_type', 'con_type');
$pChart = new UserLineChart;
$bChart = new UserLineChart;
$pChart->labels($content->values())->minimalist($display = true);
$bChart->labels($content->values());
$pChart->dataset('Connections', 'pie', [$OBD, $DC, $BSL]);
$bChart->dataset('Connections', 'bar', [$OBD, $DC, $BSL])->options([
'fill' => false,
]);
return view('chart.index', compact('pChart', 'bChart'));
}
}
I have retrieving a date and time from the database in this format:
2017-11-25 21:08:48
What I had done before using MVC was:
$date = date_create($row['article_date']);
$article_date = date_format($date, "d F Y - h:ia");
echo $article_date;
I am not sure how to do this using MVC.
My model:
public function getPosts() {
$this->db->query("SELECT `id`, `title`, `article`, `article_date`, `slug` FROM `news`");
$results = $this->db->resultSet();
return $results;
}
The controller:
public function index() {
$posts = $this->postModel->getPosts();
$data = [
'posts' => $posts
];
$this->view('posts/index', $data);
}
Then in the view I would have:
<?php foreach ($data['posts'] as $post){ ?>
// some html here
<?php echo $post->article_date; ?>
I can't just use date_format on the $post->article_date without first using date_create. But I just am not sure where I would use date_create i.e.: would you do that in the model or controller before passing to the view?
Ideally you'd do it in neither, and instead do it in the view:
<?php foreach($data['posts'] as $post){ ?>
// Some HTML here
<?php
$date = date_create($post->article_date);
$article_date = date_format($date, "d F Y - h:ia");
echo $article_date;
} // Remember to close the foreach!
?>
However, note that you don't need to mess around with date_create() at all, and can simply use date() in combination with strtotime() instead:
echo date('d F Y - h:ia', strtotime($post->article_date));
Hope this helps! :)
Is it possible to change the system date for php (only).
I want this for debugging/testing purpose.
//Test 1 (2010-01-01)
$datetime = new DateTime(); //(2010-01-01)
//Test 2 (2010-02-01)
$datetime = new DateTime(); //(2010-02-01)
I can't really change the real system clock, because other developers are working on the same system (well I could but still).
I'm just hoping this is possible, or that someone knows a nice trick.
When I print the phpinfo(); I see the following line:
Timezone Database internal
Maybe it's changeable to something like "manual" and ad a timezone with +120
Thanks!
This sounds like an encapsulation issue -- why not search for all mentions of date() or time() and replace them with $site->getDisplayedDate(), or something else appropriate to your code?
I feel your pain, but I don't think there's any way to do this. Think about it this way... how would you feel if, in some unrelated code, you called time() and it gave you some date 3 weeks in the future?
You can change only timezone (and ofcourse only couple hours forward / backward; and not whole day forward), but thats not correct solution...
You could create your own DateTime class that extends the default DateTime and hence change the behaviour:
<?php
class CustomDateTimeVariables {
public static $date = 'now';
}
class CustomDateTime extends DateTime {
public function __construct($time = null, DateTimeZone $timezone = null) {
if ($time === null) {
$time = CustomDateTimeVariables::$date;
}
if ($timezone !== null) {
parent::__construct($time, $timezone);
} else {
parent::__construct($time);
}
}
}
CustomDateTimeVariables::$date = '2010-01-01';
$datetime1 = new CustomDateTime();
CustomDateTimeVariables::$date = '2010-01-02';
$datetime2 = new CustomDateTime();
var_dump($datetime1->Format('Y-m-d H:i:s')); //"2010-01-01 00:00:00"
var_dump($datetime2->Format('Y-m-d H:i:s')); //"2010-01-02 00:00:00"
?>
DEMO
This way you can also set the default time very easily (check CustomDateTimeVariables):
<?php
class CustomDateTimeVariables {
public static $date = 'now +120 hours';
}
class CustomDateTime extends DateTime {
public function __construct($time = null, DateTimeZone $timezone = null) {
if ($time === null) {
$time = CustomDateTimeVariables::$date;
}
if ($timezone !== null) {
parent::__construct($time, $timezone);
} else {
parent::__construct($time);
}
}
}
$datetime = new CustomDateTime();
var_dump($datetime->Format('Y-m-d H:i:s')); //"2013-10-29 13:37:39"
?>
..and when it goes live, you can simply change the default back to now.