How to calculate between dates in Laravel + Vue js project - laravel

I am tired to calculate date with start & end date in my Laravel & Vue js project?
I have also installed moment js.
Code as below,
{{item.start_date | MyDate}}
{{item.end_date | MyDate}}
{{Here will be calculate between above dates}}
Please help me.

You can create new method with this logic:
methods: {
parseDate (start, end) {
return moment(start, 'YYYY.MM.DD HH:mm').diff(moment(end, 'YYYY.MM.DD HH:mm'), "days")
}
}
And in your template you can simply do that: {{ parseDate(item.start_date, item.end_date) }}
I think is that what you need. Here you can check some examples of using diff in moment.
Good luck!

If you are getting the data from Laravel you can use Carbon to calculate the difference like this:
$start_date = Carbon::parse($item->start_date);
$end_date = Carbon::parse($item->end_date);
$diff_in_hours = $end_date->diffInHours($start_date);
$diff_in_days = $end_date->diffInDays($start_date)
$diff_in_minutes = $end_date->diffInMinutes($start_date);
$diff_in_seconds = $end_date->diffInSeconds($start_date);
So if you are getting the data from a query you can map the query like this:
$schedules = DB::table('schedules')
->get()
->map(function($item){
$start_date = Carbon::parse($item->start_date);
$end_date = Carbon::parse($item->end_date);
$item->diff = $end_date->diffInMinutes($start_date);
return $item;
});
The same for models
Schedule::where('status',1)->get()
->map(function($item){
$start_date = Carbon::parse($item->start_date);
$end_date = Carbon::parse($item->end_date);
$item->diff = $end_date->diffInMinutes($start_date);
return $item;
});

Related

Save the total sum in my table (codeigniter)

Newbie at codeigniter here. how can I save my total amount in my table per user? The only thing I did is to sum it and show it on my view. My target is to save the total commission on my "agent_commission" table". I have provided a screenshot of my target for better visualization. Thank you in advance. Any help will be appreciated.
Views:
<?php $formattedNum = number_format($commCurrentBalance, 2);
echo $formattedNum;
?
Controller:
public function commi()
{
$data['activeNav'] = "";
$data['subnav'] = "commissions";
$this->header($data);
$this->nav();
$data['currentPoints']=$this->session->userdata('currentPoints');
$data['result'] = $this->commissions->get1();
$data['commCurrentBalance']=$this->load->commissions->gettotalcommi(); //getting sum of amount
$this->load->view('comm_view', $data);
$this->footer();
}
Model:
function gettotalcommi(){
$reqcommi= $this->session->userdata('uid');
$this->db->select_sum('amount');
$this->db->where('commTo',$reqcommi);
$result = $this->db->get('agent_commission_history')->row();
return $result->amount;
}
I'm not sure if it will work but please try
$db->select_sum('amount');
$db->where('commTo',$param);
$aq = $db->get_compiled_select('agent_commission_history');
$db->set('commCurrentBalance',"($aq)",false);
$db->where('commTo',$param2);
$db->update('agent_commission');
This is the Codeigniter equivalent of the query I posted earlier:
UPDATE agent_commission
SET commCurrentBalance = ( SELECT SUM(amount) FROM agent_commission_history WHERE commTo = ?)
WHERE commTo = ?
public function commi(){
$data['commCurrentBalance']=$this->load->commissions->gettotalcommi(); //getting sum of amount
$dataArray = array(
'commCurrentBalance'=>$data['commCurrentBalance']
);
$this->db->insert('agent_commission',$dataArray);
$this->load->view('comm_view', $data);
$this->footer();
}

convert Christian calendar to shamsi

i want convert date and time to persian calendar with this package :
https://github.com/hekmatinasser/verta/
this package convert to shamsi but in for loop doesnt work,
this code is run:
return new Verta($show[$i]['created_at']);
in this code show date and time to pesrian date time format
but this code doesnt work:
$ishow['message'][$i]['time'] =new Verta($show[$i]['created_at']);
in this code show with christian date time format.
my main problem is in for loop show 'created_at' with chiristian format
how can i use it?
I am interested to try the library so I tested it and it's worked with New Verta($item->created_at) or Verta::instance($item->created_at). My code is like this:
foreach ($items as $item){
$shamsi[] = Verta::instance($item->created_at);
}
dd($shamsi);
It seems your $ishow is not updated from the old form. let's try code below:
$ishow = [];
foreach ($show as $item){
$ishow['message'][]['time'] = Verta::instance($item->created_at);
}
return $ishow;
or with for loop:
$ishow = [];
for ($i = 0; $i < count($show); $i++) {
$ishow['message'][$i]['time'] =new Verta($show[$i]['created_at']);
}
return $ishow;

JSON selector `field->key` causes syntax error in laravel 5.4

I have dates like this :- $startdate= 10/23/2017 and $enddate =10/28/2017 ..need to compare with database table key name availablityschedule which is in serialize form like a:2:{s:10:"start_date";s:10:"2017-07-24";s:8:"end_date";s:10‌​:"2017-07-31";}
If all your dates are posterior to the 1st of January of 1970, you could use something like:
$startDate= date("Y-m-d");
$endDate = $JSONObject->endDate
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
if ($endDate < $startDate) { /* do Something */ }
If you are using PHP > 5.0 you can use the class DateTime
$today_dt = new DateTime($startDate);
$expire_dt = new DateTime($endDate);
if ($expire_dt < $today_dt) { /* Do something */ }
Asuming of course you would like to do this in the backend side with php.

How to set Component parameters in J2.5?

I've created a J2.5 component with some config fields using config.xml in the admin folder of the component.
How can I set parameters in the config programatically?
I've tried the code bellow, but it obviously doesn't save the result to the DB:
$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
Could anyone please show some examples of how to achieve this?
The safest way to do this would be to include com_config/models/component.php and use it to validate and save the params. However, if you can somehow validate the data params yourself I would stick with the following (much more simple solution):
// Get the params and set the new values
$params = JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
// Get a new database query instance
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query
$query->update('#__extensions AS a');
$query->set('a.params = ' . $db->quote((string)$params));
$query->where('a.element = "com_mycomponent"');
// Execute the query
$db->setQuery($query);
$db->query();
Notice how I cast the params to a string (when building the query), it will convert the JRegistry object to a JSON formatted string.
If you get any caching problems, you might want to run the following after editing the params:
From a model:
$this->cleanCache('_system');
Or, else where:
$conf = JFactory::getConfig();
$options = array(
'defaultgroup' => '_system',
'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
);
$cache = JCache::getInstance('callback', $options);
$cache->clean();
The solution is here...
http://www.webtechriser.com/tutorials/82-joomla-3-0/86-how-to-save-component-parameters-to-database-programmatically
You can replace in Joomla 2.5+ the
// check for error
if (!$table->check()) {
$this->setError('lastcreatedate: check: ' . $table->getError());
return false;
}
if (!$table->store()) {
$this->setError('lastcreatedate: store: ' . $table->getError());
return false;
}
with
if (!$table->save()) {
$this->setError('Save Error: ' . $table->getError());
return false;
}

TYPO3 Extbase: How to render the pagetree from my model?

I want to create some kind of sitemap in extbase/fluid (based on the pagetree). I have loaded the pages table into a model:
config.tx_extbase.persistence.classes.Tx_MyExt_Domain_Model_Page.mapping.tableName = pages
I have created a controller and repository, but get stuck on the part wich can load the subpages as relation into my model.
For example:
$page = $this->pageRepository->findByPid($rootPid);
Returns my rootpage. But how can I extend my model that I can use $page->getSubpages() or $page->getNestedPages()?
Do I have to create some kind of query inside my model? Or do I have to resolve this with existing functions (like the object storage) and how?
I tried a lot of things but can simply figure out how this should work.
you have to overwrite your findByPid repository-method and add
public function findByPid($pid) {
$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($querySettings);
$query = $this->createQuery();
$query->matching($query->equals('pid', $pid));
$pages = $query->execute();
return $pages;
}
to get all pages. Than you can write your own getSubpages-method like
function getSubpages($currentPid) {
$subpages = $this->pagesRepository->findByPid($currentPid);
if (count($subpages) > 0) {
$i = 0;
foreach($subpages as $subpage) {
$subpageUid = $subpage->getUid();
$subpageArray[$i]['page'] = $subpage;
$subpageArray[$i]['subpages'] = $this->getSubpages($subpageUid);
$i++;
}
} else {
$subpageArray = Array();
}
return $subpageArray;
}
i didn't test this method, but it looks like this to get alle subpages.
i wonder that i could´t find a typo3 method that return the complete Page-Tree :( So i write a little function (you can use in an extbase extension), for sure not the best or fastes way, but easy to extend or customize ;)
first you need an instance of the PageRepository
$this->t3pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
this->t3pageRepository->init();
make the init, to set some basic confs, like "WHERE deletet = 0 AND hidden = 0..."
then with this function you get an array with the page data and subpages in. I implement yust up to three levels:
function getPageTree($pid,$deep=2){
$fields = '*';
$sortField = 'sorting';
$pages = $this->t3pageRepository->getMenu($pid,$fields,$sortField);
if($deep>=1){
foreach($pages as &$page) {
$subPages1 = $this->t3pageRepository->getMenu($page['uid'],$fields,$sortField);
if(count($subPages1)>0){
if($deep>=2){
foreach($subPages1 as &$subPage1){
$subPages2 = $this->t3pageRepository->getMenu($subPage1['uid'],$fields,$sortField);
if(count($subPages2>0)){
$subPage1['subpages'] = $subPages2;
}
}
}
$page['subpages'] = $subPages1;
}
}
}
return $pages;
}

Resources