smarty and date - smarty

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);

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.

Laravel date input format validation

Is it possible to validate date input to name of day followed by a comma, then the day of the month, then the month and finally the year in full? eg Sunday, 31 December 2017
You can use the date_format validation rule with a custom defined date format string:
'some_date' => 'date_format:"l, j F Y"',
Yes, but you have to make your own custom validation function :)
More details you can find here:
custom validator
You can easily download Carbon package from packagist.org
and then use every format you want.
For example :
$dt = Carbon::create(1975, 12, 25, 14, 15, 16);
echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
You can Use Core PHP like this:
$date = '2018-01-01';
echo date('l, j F Y', strtotime($date));

Find the specific day of a given month and given year

is there a Perl module which could give me for input month and year, let say, 06-2005, what the last day of this month for this year is? For this example, it is easy, because June always has 30 days, so the last day will be 30-06-2005. But it is not the case for February. So, if I have 02-1997, I would like to know whether to return 28-02-1997 or 29-02-1997. Thanks in advance.
Yes, there is a subroutine Days_in_Month in the Date::Calc module.
use strict;
use warnings;
use Date::Calc qw/Days_in_Month/;
for my $m (1 .. 12) {
print Days_in_Month(2005, $m), "\n";
}
OUTPUT
31
28
31
30
31
30
31
31
30
31
30
31
The last day in a given month is the "0th" day of the following month. mktime() takes a 0-based month and 1900-based year number, and returns an epoch timestamp.
use POSIX qw( mktime strftime );
sub last_day {
my ( $year, $mon ) = #_;
return mktime(0,0,0, 0, $mon, $year-1900);
}
You can pass that to localtime or strftime.
say scalar localtime last_day(2005, 5)
'Tue May 31 00:00:00 2005'
say scalar localtime last_day(2005, 6)
'Thu Jun 30 00:00:00 2005'
say scalar localtime last_day(1997, 2)
'Fri Feb 28 00:00:00 1997'
say scalar localtime last_day(2012, 2)
'Wed Feb 29 00:00:00 2012'
say (localtime last_day(1997, 2))[3]
'28'
say strftime "%d", localtime last_day(1997, 2)
'28'
As others said, use DateTime:
use DateTime;
my $dt = DateTime->last_day_of_month('year'=>2000, 'month'=>2);
print $dt->ymd; # '2000-02-29'
While DateTime may not be the fastest module for handling dates/times, it's definitely the most complete one. It's also the only one that handles the various quirks of date/time math correctly.

How do I output to the following format: month - year?

Today's month is November (11). With 1.years.ago.to_date..Date.today how can I output:
11 - 2010, 12 - 2010, 01 - 2011, 02 - 2011, 03 - 2011, etc
strftime
Use function for all date modifications in ruby
Refer This DOC
There's probably a more efficient way to do this, but this will give you the output you want:
require "active_support/core_ext/integer/time"
((1.year.ago.to_date)..(Date.today)).map { |d| d.strftime("%m-%Y") }.uniq!
For print date used strtotime() function.
//For today print a date used the following code
echo date('m/d/Y',strtotime("today"));
//For one year ago print a date used the following code
echo date('m.d.Y',strtotime("-1 years"));
//For coming year date from today used following code
echo date('m.d.Y',strtotime("1 years"));
You can to add a new format to your locales.
#/config/locales/en.yml
en:
date:
formats:
month_year: "%m - %Y"
and to use it with I18n.l(your_date, :format => :month_year)
This will help if you want to change the format later, you will change in a unique point.

How can I just get the "Year" portion from the output of timespan( ) in 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];

Resources