Smarty if statement with date? - smarty

If I have a smarty variable
{$test.date_created}
that displays a date in a smarty template, for example:
02/2/2012
On the smarty website, it says that you can display today's date with the following.
{$smarty.now|date_format:'%m/%d/%Y'}
How can I use it in an if statement to display a message when its 3 days old or more from today's date?
If its 3 days old or more, display "Old". If not, Display "New".

You can use strtotime() to get the timestamp corresponding to three days ago, and then compare this to the date of your message. For example, assuming $message is your message record and $message['date'] is the timestamp you have to check:
$isMessageOld = ($message['date'] <= strtotime('-3 days'));
$smarty->assign('isMessageOld', $isMessageOld);
And then, in your template:
{if $isMessageOld} ... {/if}
I'm not 100% sure, but you can also test it directly in Smarty. Assuming you have $message passed to Smarty:
{if $message.date <= strtotime('-3 days')} ... {/if}

You can easily check already passed date in smarty by using this $smarty.now
$smarty.now|date_format:"%Y%m%d"
Here is an example of cross the old(passed) date
<{foreach from=$meetings item=m}>
<{if $smarty.now|date_format:"%Y%m%d" <= $m.date|date_format:"%Y%m%d"}>
<tr>
<td><{counter}></td>
<td><{$m.title}> on </td>
<td><{$m.date}></td>
</tr>
<{else}>
<tr>
<td><strike><{counter}></strike></td>
<td><strike><{$m.title}> on </strike></td>
<td><strike><{$m.date}></strike></td>
</tr>
<{/if}>
<{/foreach}>
We need to create meeting list array in php and assign it in smarty
$meetings[0]['title'] = "Speech on Gandhi Janyanti";
$meetings[0]['date'] = "2-Oct-2011";
$meetings[1]['title'] = "Meet friend";
$meetings[1]['date'] = "10-Oct-2013";
$meetings[2]['title'] = "Goto USA";
$meetings[2]['date'] = "22-Oct-2013";
$meetings[3]['title'] = "Speech on Gandhi Janyanti";
$meetings[3]['date'] = "2-Oct-2014";
$meetings[4]['title'] = "Meeting with John";
$meetings[4]['date'] = "22-Oct-2014";
$meetings[5]['title'] = "Speech on Gandhi Janyanti";
$meetings[5]['date'] = "2-Oct-2015";
$meetings[6]['title'] = "Meeting with Uncle";
$meetings[6]['date'] = "22-Oct-2015";
$theme->assign("meetings",$meetings);
echo $theme->fetch("index.tpl");

Related

How to create a list of due dates which are dynamically added base from approved date in Laravel?

I am doing a credit collection app which offer loans to the borrowers. I've been stock in this for so many days now because I can't think of a better way to do it.
SO basically I want to achieve is to have a list of due dates which I will display in the borrower dashboard. for example, the borrower applied for a 2 months loan duration and bi-monthly payment scheme and let say the admin approve it today. So what I want is base from the date today, 12-10-2021 the system will generate 4 due dates(12-25-2021, 12-8-2021, 1-23-2022 and 02-7-2022) and will save them to a separate due dates table.
With regards to the payments, I can analyze the part where the system can check if the payment is done on or before the first due date but how can I tell to the system that the payment is advance payment for the next 3 due date?
what I only have right now is this
$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');
Which can identify the first due date.
Can you help to point to any resources out there than will help me achieve my objective? I am hoping I was able to describe the question and my objective clearly. Any suggestions will be very much appreciated. Thank you so much in advance!
RESPONSE FOR #GRANT
I am following your code
$date_approval = Carbon::createFromTimestamp(strtotime($loanapplication->date_approval));
$scheme_numdays = $loanapplication->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays;
$due_date = $date_approval->addDays($days)->format('Y-m-d');
$due_date_plus = $date_approval->addDays(60)->format('Y-m-d');
# Carbon Period usage:
$result = CarbonPeriod::create($due_date, $scheme_numdays.' days', $due_date_plus);
I replace the '# days' in the $result with $scheme_numdays so which contains the number of days.
Then I display it like this,
<div class="col-md-6">
{{$date_approval}}<br/>
{{$loanapplication->date_approval}}<br/>
#foreach ($result as $dt)
{{$dt->format("M d Y")}} <br/>
#endforeach
</div>
the result is
2022-02-27 00:00:00
2021-12-14
Dec 29 2021
Jan 13 2022
Jan 28 2022
Feb 12 2022
Feb 27 2022
Question:
why {{$date_approval}} (2022-02-27 00:00:00) and {{$loanapplication->date_approval}} (2021-12-14) gave different result? The date 2021-12-14 is what I have in my database.
My second question is why the result is five instead of 4 only?
The Data I'm displaying is, the loan duration is 60 days and the payment scheme is on every 15 days or fortnightly.
CarbonPeriod would be perfect for this particular scenario. Have you tried the following:
$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('Y-m-d');
$due_date_plus = $date_approval->addMonths(8)->format('Y-m-d');
# Carbon Period usage:
$result = CarbonPeriod::create($due_date, '2 months', $due_date_plus);
foreach ($result as $dt) {
echo $dt->format("M d Y");
}
You may need to namespace it:
use Carbon\CarbonPeriod;
Edited answer for followup questions:
<div class="col-md-6">
{{ $date_approval->format('M d Y') }}<br/>
{{ $loanapplication->date_approval->format('M d Y') }}<br/>
#foreach ($result as $dt)
#if($loop->index > 3)
#break
#endif
{{ $dt->format('M d Y') }}<br />
#endforeach
</div>
An alternative is date casting when getting dates from models.
(in loan application model):
protected $casts = [
'date_approval' => 'datetime:M d Y',
];

Converting date to mothname, day, year with like() codeigniter 4

I'm using ci4 and I want to get the month name on search with like() method:
I tried this but did not work:
table column = p.date = '2021-05-18 01:07:50'
$this->like(date('F', strtotime('p.date')), date('F', strtotime('18-05-2021'));
//createdon - 2020-06-15 02:19:40
$this->db->where('month(createdon)',6);
$result = $this->db->get('yourtablename')->result();

Magento 2 - wrong number of digits between separators for prices in Indian Rupees

I am using Magento 2.2.3. my default currency is INR, but it shows in the wrong format:
But it should be ₹77,65,000.00. How do we correct price format? Currently its wrong... like USD.
You can set the currency format by following code.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper
$price = 1000; //Your Price
$formattedPrice = $priceHelper->currency($price, true, false);
?>
File path: vendor/magento/zendframework1/library/Zend/Locale/Data/en.xml
On line number 3353, under section currencyFormat and type = "standard", change the pattern from <pattern>¤#,##0.00</pattern> to <pattern>¤ #,##,##0.00</pattern>
Still, on PDP page and cart page summary the price format does not change because the prize format is coming from the JS in which Magento using a RegExp function for only US price format.
For that, please change the code in the below file.
File path: vendor/magento/module-catalog/view/base/web/js/price-utils.js (First extend this file in your theme directory and do the respected changes)
Under the function formatPrice below this line comment all the line in the respective function.
i = parseInt(
amount = Number(Math.round(Math.abs(+amount || 0) + 'e+' + precision) + ('e-' + precision)),
10
) + '';
And add this set of code below the above line.
var x=i;
x=x.toString();
var afterPoint = '';
if(x.indexOf('.') > 0)
afterPoint = x.substring(x.indexOf('.'),x.length);
x = Math.floor(x);
x=x.toString();
var lastThree = x.substring(x.length-3);
var otherNumbers = x.substring(0,x.length-3);
if(otherNumbers != '')
lastThree = ',' + lastThree;
var response = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree + afterPoint;
return pattern.replace('%s', response);
deploy and `rm -rf var/cache/*
And then you're done. For Example: A price previously displayed like 453,453, will now display in the Indian manner like 4,53,453.

Smarty check if date is between

i need check if smarty now date is equal or between my two dates.
If dates is the same months its work. But if:
Start = 19.01.2015 and
Stop = 1.02.2015 and
smarty now is 19.01.2015 it show no. Only if i change months it don't work
{if ($smarty.now|date_format:"%d.%m.%Y") >= ($value->getVariableValue('Start')) AND ($smarty.now|date_format:"%d.%m.%Y") <= ($value->getVariableValue('Stop'))}
yes
{else}
no
{/if}
try this i hope it will work :
Php file :
<?php
$start_date = "19.01.2015";
$end_date = "1.02.2015";
$smarty->assign('start', $start_date);
$smarty->assign('stop', $end_date);
$smarty->display("date.tpl");
?>
tpl file(date.tpl) :
<{if (($smarty.now|date_format:"%d.%m.%Y") >= ($start)) AND (($smarty.now|date_format:"%d.%m.%Y") <= ($stop)) }>
yes
{else}
no
{/if}

How to extract links, text and timestamp from webpage via Html Agility Pack

I am using Html Agility Pack and are trying to extract the links and link text from the following html code. The webpage is fetched from a remote page and the saved locally as a whole. Then from this local webpage I am trying to extract the links and link text. The webpage naturally has other html code like other links text, etc inside its page but is removed here for clarity.
<span class="Subject2"><a href="/some/today.nsf/0/EC8A39D274864X5BC125798B0029E305?open">
Description 1 text here</span> <span class="time">2012-01-20 08:35</span></a><br>
<span class="Subject2"><a href="/some/today.nsf/0/EC8A39XXXX264X5BC125798B0029E312?open">
Description 2 text here</span> <span class="time">2012-01-20 09:35</span></a><br>
But the above are the most unique content to work from when trying to extract the links and linktext.
This is what I would like to see as the result
<link>/some/today.nsf/0/EC8A39D274864X5BC125798B0029E305</link>
<title>Description 1 text here</title>
<pubDate>Wed, 20 Jan 2012 07:35:00 +0100</pubDate>
<link>/some/today.nsf/0/ EC8A39XXXX264X5BC125798B0029E312</link>
<title>Description 2 text here</title>
<pubDate> Wed, 20 Jan 2012 08:35:00 +0100</pubDate>
This is my code so far:
var linksOnPage = from lnks in document.DocumentNode.SelectNodes("//span[starts-with(#class, 'Subject2')]")
(lnks.Name == "a" &&
lnks.Attributes["href"] != null &&
lnks.InnerText.Trim().Length > 0)
select new
{
Url = lnks.Attributes["href"].Value,
Text = lnks.InnerText
Time = lnks. Attributes["time"].Value
};
foreach (var link in linksOnPage)
{
// Loop through.
Response.Write("<link>" + link.Url + "</link>");
Response.Write("<title>" + link.Text + "</title>");
Response.Write("<pubDate>" + link.Time + "</pubDate>");
}
And its not working, I am getting nothing.
So any suggestions and help would be highly appreciated.
Thanks in advance.
Update: I have managed to get the syntax correct now, in order to select the links from the above examples: With the following code:
var linksOnPage = from lnks in document.DocumentNode.SelectNodes("//span[#class='Subject2']//a")
This selects the links nicely with url and text, but how do I go about also getting the time stamp?
That is, select the timestamp out of this:
<span class="time">2012-01-20 09:35</span></a>
which follows each link. And have that output with each link inside the output loop according to the above? Thanks for any help in regards to this.
Your HTML example is malformed, that's why you get unexpected results.
To find your first and second values you'll have to get the <a> inside your <span class='Subject2'> - the first value is a href attribute value, the second is InnerText of the anchor. To get the third value you'll have to get the following sibling of the <span class='Subject2'> tag and get its InnerText.
See, this how you can do it:
var nodes = document.DocumentNode.SelectNodes("//span[#class='Subject2']//a");
foreach (var node in nodes)
{
if (node.Attributes["href"] != null)
{
var link = new XElement("link", node.Attributes["href"].Value);
var description = new XElement("description", node.InnerText);
var timeNode = node.SelectSingleNode(
"..//following-sibling::span[#class='time']");
if (timeNode != null)
{
var time = new XElement("pubDate", timeNode.InnerText);
Response.Write(link);
Response.Write(description);
Response.Write(time);
}
}
}
this outputs something like:
<link>/some/today.nsf/0/EC8A39D274864X5BC125798B0029E305?open</link>
<description>Description 1 text here</description>
<pubDate>2012-01-20 08:35</pubDate>
<link>/some/today.nsf/0/EC8A39XXXX264X5BC125798B0029E312?open</link>
<description>Description 2 text here</description>
<pubDate>2012-01-20 09:35</pubDate>

Resources