How can I just get the "Year" portion from the output of timespan( ) in CodeIgniter? - codeigniter

I have a "Date of Birth" field, and trying to use the timespan() method to get the age in years. But returns "28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes".
Any idea how I can just get the "28 Years" portion?

I suggest you use PHP's strftime() function. Instead of using the CI's timespan().
echo strftime('%Y', 1226239392);

There are many ways to do this, with the string in $date, like so:
$date = '28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes';
This will give you "28 Years"
$yearsPart = substr($date, 0, strpos($date, 'Years') + 5);
So will this:
$parts = split(', ', $date);
$yearsPart = $parts[0];

Related

How to create a proper calculation of a due date in a dynamic manner in laravel

I been analyzing the proper calculation of the duedate for my app. I am working with a lending app where I need to display the due date for the borrower.
let say the approved date of their loan is 2019-10-27 and today is 11-5-2019. the payment scheme is dynamic. depending on what the admin set. in this example, the payment scheme weekly so I just need to say;
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
from my example above the due date is supposedly on Nov 3, 2019. Now how can I make it say that the next due date is on Nov 10, 2019?
I really tried to analyze, here's my thought.
I get the difference between the approval date and NOW()
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
now I compared the $diff with $loan_application->scheme->num_days. so let say the $diff=9 and $loan_application->scheme->num_days = 7
now I created the condition
if ($diff > $loan_application->scheme->num_days) {
//should display
Nov 10, 2019
//because the current date is already Nov 5
}
and after Nov 10, + 7 days again, and + 7 days again and so on.
here is the complete code I already have;
$dateApproved = Carbon::parse($loan_application->date_approval)->toFormattedDateString();
$now = Carbon::today('M d Y');
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
$numOfScheme = $loan_application->loanDuration->num_days / $loan_application->scheme->num_days;
if ($diff > $loan_application->scheme->num_days) {
}
I hope you understand my question. Please help. Thank you so much in advance.
this will add number of days from payment scheme. in my example 7 days. but how can I tell my code that today is already 2 days late, add additional 7 days
It took some time but I understood your question.
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('M d Y');
intdiv is just php's integer division.

How to run laravel cron after every 2 weeks or 14 days

I need to run laravel cron for after every 2 weeks or 14 days. but not found any solution for this.
I was also search on laravel documentation https://laravel.com/docs/5.5/scheduling
and found this option
->weekly();
But it runs on weekly.
I was search also other option and found this
laravel schedule task to run every 10 days of a month
But it work only first 10 days of a month
$schedule->command('log:test')->cron('0 0 1-10 * *');
Please help me if you have any solution thanks in advance.
$schedule->command('command_name')->weekly()->mondays()
->when(function () {
return date('W') % 2;
})->at("13:38");
Function will return 1 in every two other weeks, So the command will be called in every two weeks.
You can use daily() together with when() method and inside when add suitable constrain for your task. For example, run task every month on 1,16 days:
$schedule->command('sitemap:sitemap_xml_generate')->daily()->when(function () {
$days = [1,16];
$today = Carbon::today();
return in_array($today->day, $days);});
or you can use this
$schedule->command('sitemap:sitemap_xml_generate')->cron('0 0 1,16 * *');
cron('0 0 1,16 * *') -> Argument are 'minute hour day(we can specify multiple date seperated by comma) month year'
$schedule->command('log:test')->cron('0 0 */14 * *');
2017-09-15 00:00:00
2017-09-29 00:00:00
2017-10-01 00:00:00
2017-10-15 00:00:00
2017-10-29 00:00:00
or you can run your custom
$schedule->command('log:test')->weekly()->when(function () {
return ((Carbon::now()->day % 2) === 1); //bool only for example
});

Time ago in words convert into system date-time

Trying to convert strings like 9 weeks ago, 1 year, 6 months ago, 20 hours ago into a ruby time object like Tue, 10 Mar 2015 12:06:15 PDT -07:00.
I've been doing this:
eval("10 days ago".gsub(' ', '.'))
This works fine, but for strings like 1 year, 6 months ago blows up.
I just need to do comparisons like:
eval("10 days ago".gsub(' ', '.')) < (Time.now - 7.days)
I'm using sinatra so no fancy rails helpers.
Please never use eval in production code..
Converting from timeago notation would be quite complex and resource intensive.
However, this way seems the least error prone: It will convert a string like "5 seconds ago" to "5S" and use mapping to find what it means in time, after which it will subtract that time from the current time.
The parse string is dynamically built so it can accomodate most every timeago notation.
require('date')
mapping = {"D"=> "%d","W"=>"%U","H"=>"%T","Y"=>"%Y","M"=>"%m","S"=>"%S"}
timerel = "1 year, 6 months ago".split(",").map { |n| n.gsub(/\s+/, "").upcase()[0,2].split('')}
Date.strptime(
timerel.map {|n| n[0]}.join(" "),
timerel.map {|n| mapping[n[1]]}.join(" ")
)
date = Date.new(0) + (Date.today - Date.strptime(timerel.map {|n| n[0]}.join(" "), timerel.map {|n| mapping[n[1]]}.join(" ")))
=> #<Date: 2014-10-10 ((2456941j,0s,0n),+0s,2299161j)>
It goes without saying that is very error prone. Use at your own risk:
def parse(date:)
eval(date.gsub(/ ?(,|and) ?/, '+').tr(' ', '.').gsub(/^(.*)(\.ago)$/, '(\1)\2'))
end
parse(date: '1 year, 6 months ago') # => Wed, 10 Sep 2014 21:29:11 BST +01:00
parse(date: '1 year, 6 months, 3 weeks, 6 days, 9 hours and 12 seconds ago')
# => Thu, 14 Aug 2014 12:33:07 BST +01:00
The idea is to convert the original string to:
'(1.year+6.months).ago'

NEXT_DAY in Crystal Reports

Is there anything like the Oracle "NEXT_DAY" function available in the syntax that Crystal Reports uses?
I'm trying to write a formula to output the following Monday # 9:00am if the datetime tested falls between Friday # 9:00pm and Monday # 9:00am.
So far I have
IF DAYOFWEEK ({DATETIMEFROMMYDB}) IN [7,1]
OR (DAYOFWEEK({DATETIMEFROMMYDB}) = 6 AND TIME({DATETIMEFROMMYDB}) in time(21,00,00) to time(23,59,59))
OR (DAYOFWEEK({DATETIMEFROMMYDB}) = 2 AND TIME({DATETIMEFROMMYDB}) in time(00,00,00) to time(08,59,59))
THEN ...
I know I can write seperate IF statements to do a different amount of DateAdd for each of Fri, Sat, Sun, Mon, but if I can keep it concise by lumping all of these into one I would much prefer it. I'm already going to be adding additional rules for if the datetime falls outside of business hours on the other weekdays so I want to do as much as possible to prevent this from becoming a very overgrown and ugly formula.
Since there is no CR equivalent that I know of, you can just cheat and borrow the NEXT_DAY() function from the Oracle database. You can do this by creating a SQL Expression and then entering something like:
-- SQL Expression {%NextDay}
(SELECT NEXT_DAY("MYTABLE"."MYDATETIME", 'MONDAY')
FROM DUAL)
then you could either use that directly in your formula:
IF DAYOFWEEK ({MYTABLE.MYDATETIME}) IN [7,1]
OR (DAYOFWEEK({MYTABLE.MYDATETIME}) = 6 AND TIME({MYTABLE.MYDATETIME}) in time(21,00,00) to time(23,59,59))
OR (DAYOFWEEK({MYTABLE.MYDATETIME}) = 2 AND TIME({MYTABLE.MYDATETIME) in time(00,00,00) to time(08,59,59))
THEN DateTime(date({%NextDay}),time(09,00,00))
Or, the even better way would be to just stuff ALL of the logic into the SQL Expression and do away with the formula altogether.
Considering Sunday is 1
And the first 7 is the week we want to back
7 = 1 week
14 = 2 weeks
The last Number (1) is 1 for Sunday, 2 for Monday, 3 for Tuestday
Last Sunday 1 week ago
Today - 7 + ( 7 - WEEKDAY(TODAY) )+1
Last Monday 2 weeks ago
Today - 14 + ( 7 - WEEKDAY(TODAY) )+2
So this 2 formulas give me MONDAY LAST WEEK and SUNDAY LAST WEEK.
EvaluateAfter({DATETIMEFROMMYDB}) ;
If DayOfWeek ({DATETIMEFROMMYDB}) In [crFriday,crSaturday,crSunday,crMonday]
then
IF DayOfWeek ({DATETIMEFROMMYDB}) In [crFriday]
AND TIME({DATETIMEFROMMYDB}) >= time(21,00,00)
then //your code here
Else if Not(DayOfWeek ({DATETIMEFROMMYDB}) In [crFriday] )
AND (TIME({DATETIMEFROMMYDB}) >= time(00,00,00) AND TIME({DATETIMEFROMMYDB}) <= time(23,59,59))
then //your code here
Else if DayOfWeek ({DATETIMEFROMMYDB})In [crMonday]
AND TIME({DATETIMEFROMMYDB}) < time(09,00,00)
then //your code here

smarty and date

i get date with: {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}
But how get 20 day after?
If now: 2010 05 05 12:12:12, I wish to show 2010 25 05 12:12:12
{$smarty.now} is a simple timestamp (number of seconds since 1970). So you can just add as many seconds to it as you need:
{$smarty.now+20*24*60*60|date_format:'%Y-%m-%d %H:%M:%S'} //+20 days
This works in Smarty3, if not in older versions then you might need to do the math with {assign} and/or {math} directives.
Use the strtotime() php function and assign your variable to smarty. Something like this:
<?php
$later = strtotime('+20 day');
$smarty->assign('later', $later);
?>
Then in the template:
{ $later|date_format:'%Y-%m-%d %H:%M:%S'}
You can use strtotime() directly as a modifier.
{"+20 days"|strtotime|date_format:"Y/m/d"}
In newer versions of smarty it will strtotime any string you prepend
I.e. instead of doing {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} you can also do {"now"|date_format:'%Y-%m-%d %H:%M:%S'}
To get the date 20 days from now, you can do:
{"+20 days"|date_format:"%Y-%m-%d"}
{assign var="iItemOne" value=$smarty.now}
{assign var="iItemTwo" value=1296000} //60*60*24*15-> for 15 days
{assign var="iSum" value=$iItemOne+$iItemTwo}
{$iSum|date_format:'%Y-%m-%d %H:%M:%S'}
Tested in smarty : Add 1 day ,2 days ......365 days in dynamic date.
$one= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 1 day"));
$this->smarty->assign('one',$one);
$two= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 2 day"));
$this->smarty->assign('two',$two);
...
..
$oneyear= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 365 day"));
$this->smarty->assign('oneyear',$oneyear);

Resources