View previous topic :: View next topic |
Author |
Message |
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Tue Dec 27, 2016 2:25 am Post subject: modifying a Date, having issues with the strtotime. |
|
|
So in continuing with my conversion of a calendar project I ran into this issue.
This code performs correctly:
{$todaysdate = date('Y-m-d')}
{$todaysdate}<br />
{$testtest = strtotime($todaysdate)}
{$testtest}<br />
{$futuredate = date("Y-m-d",$testtest)}
{$futuredate}<br />
output:
2016-12-26
1482739200
2016-12-26
----
But I am actually trying to adjust the date using the STRTOTIME php function. It works in PHP but not in Smarty - so far.
{$todaysdate = date('Y-m-d')}
{$todaysdate}<br />
{$testtest = strtotime("+364 days",$todaysdate)}
{$testtest}<br />
{$futuredate = date("Y-m-d",$testtest)}
{$futuredate}<br />
gets me:
2016-12-26
Notice: A non well formed numeric value encountered in /var/www/ht...
31451616
1970-12-30
....
This is the first php function I have met so far that I can not get working as it does in php.
{$testtest = strtotime("+364 days",$todaysdate)} |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 27, 2016 2:56 am Post subject: |
|
|
Do. Not. Use. date() in an environemnt, where you would need to use localization or compare dates across timezones.
Use DateTime class and derivatives.
Something along the lines of
Code: | $nf = new \NumberFormatter("ru_RU", \NumberFormatter::CURRENCY);
$cs = ini_get('default_charset');
$smarty->registerPlugin('modifier', 'price',
function($price, $currency = 'RUB') use($nf, $cs)
{
return iconv('UTF-8', $cs, $nf->formatCurrency($price, $currency));
}
); |
And while you're at it, note that if you don't explicitly specify time, it will pick your current time, which may be another source of frustration in modern PHP.
With that said, what you're doing wrong is placing your business logic into templates.
Pass the date to the template, don't calculate it. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Dec 27, 2016 3:01 am Post subject: |
|
|
strtotime() requires an integer as the second argument:
strtotime ( string $time [, int $now = time() ] ) |
|
Back to top |
|
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Tue Dec 27, 2016 3:49 am Post subject: Thanks |
|
|
I do actually declare the localization in my code.
the code is looping through a large amount of dates looking for matches for different conditions and changing display output based on matches.
I pass the array of dates to the tpl via an array ultimately but given the nature of conditions where formatting is applied to varying dates, working it in the tpl would be much more preferred.
...
EDIT:
ok got it. Was mixing myself up.
{$todaysdate = date('Ymd')}
{$todaysdate}<br />
{$futuredate = date("Y-m-d",strtotime("+364days",strtotime($todaysdate)))}
{$futuredate}<br />
output:
20161226
2017-12-25
Thanks for helping me think it through.
Last edited by zewtastic on Tue Dec 27, 2016 3:56 am; edited 1 time in total |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 27, 2016 3:54 am Post subject: |
|
|
Yet again: DO NOT EVEN LOOK at string dates. At all. Stop it. Don't compare them. |
|
Back to top |
|
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Tue Dec 27, 2016 3:58 am Post subject: thanks |
|
|
I got it working, see above.
I have to look at string dates, that is what I am loading from the DB.
I was just using date('Ymd') in this example to generate a starting value.
EDIT:
But maybe I should convert my script from recording YYYY-MM-DD values to timestamps. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Dec 27, 2016 5:07 am Post subject: |
|
|
I work on an application that databases both Epoch timestamps as well as string dates.
I don't know why. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 27, 2016 6:07 am Post subject: Re: thanks |
|
|
zewtastic wrote: | string dates, that is what I am loading from the DB. |
WHAAAAAAAAT??????????
Fix your DB. Now.
You MUST be storing actual dates.
Not random strings. Not random integers.
Either DATETIME or TIMESTAMP, or any other appropriate field type, but it must be directly comparable as date. |
|
Back to top |
|
|