<![CDATA[ Show current date without a snippet - MODX Community Forums]]> https://forums.modx.com/thread/?thread=94876 <![CDATA[Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513749
I wanted to put the current year in the footer of my site, without bothering with a snippet. (If there are any magic settings containing the current time, I could not recall. Maybe there are.) But here's a simple way to do it in any case:

[[!+nowdate:default=`now`:strtotime:date=`%Y`]]


The "nowdate" placeholder doesn't really exist, but giving it a default value of "now" gets you a value to pass along. Pass that to the strtotime filter, you get the current unix timestamp. Pass that to the date filter, you get any representation of the current date you want.
]]>
netProphET Oct 30, 2014, 11:01 PM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513749
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-549829 Quote from: netProphET at Oct 30, 2014, 11:01 PM
Just a quick tip while it's in my head (from updating my band's sadly neglected site).

I wanted to put the current year in the footer of my site, without bothering with a snippet. (If there are any magic settings containing the current time, I could not recall. Maybe there are.) But here's a simple way to do it in any case:

[[!+nowdate:default=`now`:strtotime:date=`%Y`]]


The "nowdate" placeholder doesn't really exist, but giving it a default value of "now" gets you a value to pass along. Pass that to the strtotime filter, you get the current unix timestamp. Pass that to the date filter, you get any representation of the current date you want.

This is One Percent Pure Genius - of the rarest kind!
And Ninety-Nine Percent Perspicuity!]]>
donshakespeare Apr 03, 2017, 06:12 AM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-549829
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513773 sottwell Oct 31, 2014, 04:21 AM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513773 <![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513763 Quote from: sottwell at Oct 31, 2014, 06:25 AM
Just be aware that it will be a lot more processing-intensive, and thus slower, than a simple snippet, like the CopyYears snippet, which will display the current year or a range of years, like '2000-2014'
$now = date("Y");
$startYear = $modx->getOption('startYear', $scriptProperties, $now);
$spacer = $modx->getOption('spacer', $scriptProperties, '-');
$years = ($now > $startYear) ? $startYear.$spacer.$now : $now;
return $years;

have a snippet 'loop'

$loops = $modx->getOption('loops',$scriptProperties,10);
$tpl = $modx->getOption('tpl',$scriptProperties,'');

for ($i=0; $i < $loops; $i++){
    $p['idx'] = $i;
    $output[] = $modx->getChunk($tpl,$p);
}

$output = implode('<br />',$output);

return $output;


another snippet 'nowdate':
return strftime('%Y-%m-%d %H:%M:%S');


I call it:

[[!loop? &loops=`10000` &tpl=`nowdate`]]


having in the nowdate-chunk:
[[!+nowdate:default=`now`:strtotime:date=`%Y-%m-%d %H:%M:%S`]]


first output: 2014-10-31 07:09:09
last output: 2014-10-31 07:09:20

having in the nowdate-chunk:
[[!nowdate]]


first output: 2014-10-31 07:21:34
last output: 2014-10-31 07:21:50


So, I don't know why,
but with the output-filter-version, I have allways about 10secs between the first and last output.
With the snippet-version, I have allways about 15secs for 10000 loops.


When I replace getChunk in the snippet with strftime directly:

$loops = $modx->getOption('loops',$scriptProperties,10);
$tpl = $modx->getOption('tpl',$scriptProperties,'');

for ($i=0; $i < $loops; $i++){
    $p['idx'] = $i;
    $output[] = strftime('%Y-%m-%d %H:%M:%S');
}

$output = implode('<br />',$output);

return $output;


there is no difference between the first and last output, so much less than one second without the getChunk - parsing - process.





]]>
Bruno17 Oct 31, 2014, 02:29 AM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513763
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513761
$now = date("Y");
$startYear = $modx->getOption('startYear', $scriptProperties, $now);
$spacer = $modx->getOption('spacer', $scriptProperties, '-');
$years = ($now > $startYear) ? $startYear.$spacer.$now : $now;
return $years;
]]>
sottwell Oct 31, 2014, 01:25 AM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513761
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513760 Thanks for sharing
Would be another good example for chaining multiple output-filters at rtfm:
]]>
Bruno17 Oct 31, 2014, 12:43 AM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513760
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513755
[[+nowdate:default=`2 years ago`:strtotime:date=`%Y`]]



Examples from here http://php.net/manual/en/function.strtotime.php

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
]]>
sottwell Oct 30, 2014, 11:28 PM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513755
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513753

I imagine instead of `now` you could probably use `tomorrow` `yesterday` `next week` `next thursday` etc.]]>
BobRay Oct 30, 2014, 11:20 PM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513753
<![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513752 ]]> netProphET Oct 30, 2014, 11:19 PM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513752 <![CDATA[Re: Show current date without a snippet]]> https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513751 sottwell Oct 30, 2014, 11:18 PM https://forums.modx.com/thread/94876/show-current-date-without-a-snippet#dis-post-513751