CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Date: over a year, under a month, a few days etc  (Read 2618 times)

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1054
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Date: over a year, under a month, a few days etc
« Reply #15 on: 31 Jan 2020, 12:54:09 pm »
I also forgot to mention that leveraging zero as "loose false" and non-zero as "loose true" simplified the code a bit too. MOST of the time if you're sure the value is numeric, there's no real reason to say ==0 or !=0 in loosely typed languages. Just if() it.

Here's what I meant by using define, it's PHP 7 / newer only but that should be a non issue as of two years ago.

Code: [Select]
// defining Arrays is new as of PHP 7

define('DATE_WORDS', [
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second'
]);

function date_CalculateDifference($incoming) {

// calculates the difference between DateTime objects
$interval = date_diff(
empty($incoming['end']) ? date() : date_create($incomping['end']),
date_create($incoming['start'])
);

foreach ($interval as $k => $v) {
if ($v) return pluralize($v, DATE_WORDS[$k]) . (
$interval->invert ? ' ago' : ''
);
}

return 'now'; // they were all zero

} // date_calcDifference

function pluralize($val = 0, $text) {

return $val . ' ' . $text . ($val == 1 ? '' : 's';

} // pluralize

Why do this? First off it gets excess code out of the logic of your function. Second you might want/need it elsewhere so you only have one copy. Third it makes it static in memory so that every time you call your function it doesn't get recreated / take extra processing time. Finally it gives you a nice central spot to change it across all functions you might need it for when it comes to things like localization.

Basically if you were to call your difference function more than once, putting a static value into define (or a static on an object) uses less memory and runs faster, and requires less garbage collection.
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Date: over a year, under a month, a few days etc
« Reply #16 on: 31 Jan 2020, 01:31:18 pm »
I am making the the switch to single quotes and as you can see I’m still adjusting... sorry!

Thanks again for the feedback Jason. I appreciate it!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Date: over a year, under a month, a few days etc
« Reply #17 on: 31 Jan 2020, 02:48:57 pm »
Thanks - I spotted and corrected the intentional typos - I hope I pass the test! :)

I am using PHP 7.4.1 currently - although I haven't checked for updates recently...

Code: [Select]
<?php

// defining Arrays is new as of PHP 7

define('DATE_WORDS', [
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second'
]);

function 
date_CalculateDifference($incoming) {

// calculates the difference between DateTime objects
$interval date_diff(

empty( $incoming['end']) ? date_create(date("y-m-d H:i:s")) : date_create($incoming['end']),

date_create($incoming['start'])

);

foreach ($interval as $k => $v) {

if( $v ) { 

return pluralize($vDATE_WORDS[$k]) . ( $interval->invert ' ago' '' ); 

}



}

return 'now'// they were all zero

// date_calcDifference

function pluralize($val 0$text) {

return $val ' ' $text . ($val == '' 's');

// pluralize

Thanks Jason - sorry for spamming your forums!

It works a  treat now:
Code: [Select]
<form method="post" action="?module=UserCP&amp;action=ChangePassword">

<h1>Change Password</h1>

<input type="hidden" name="formkey" value="5c38a1a1436a6039c86e2b80c4251a4276bb8aa0a5557c08">

<input type="hidden" name="MODE" value="01">

<input type="hidden" name="user" value="1">

<fieldset>

<legend>Current Password</legend>

<p>Last Change: 2 minutes ago  </p>

<div>

<label for="password">Current Password: </label>

<input id="password" type="password" name="current_password" value="" placeholder="Current Password">

</div>

</fieldset>

<fieldset>

<legend>New Password</legend>

<div>

<label for="new_password">New Password: </label>

<input id="new_password" type="password" name="new_password" value="" placeholder="New Password">

</div>

<div>

<label for="new_password">Confirm Password: </label>

<input id="new_password2" type="password" name="new_password2" value="" placeholder="Confirm Password">

</div>

</fieldset>

<fieldset>

<legend>Update Options</legend>

<button>Change Password</button>

</fieldset>

</form>
« Last Edit: 31 Jan 2020, 03:04:16 pm by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

ian

  • Keeping on
  • Global Moderator
  • Junior Member
  • *****
  • Posts: 32
  • Karma: +1/-0
Re: Date: over a year, under a month, a few days etc
« Reply #18 on: 31 Jan 2020, 05:13:39 pm »
Oh and I forgot to mention this earlier, pluralise is not a word.
Pluralize "Give a plural form to (a word)" the spelling using a "z" in place of and "s" is (contrary to that link) correct according to my Oxford English Dictionary [revised 1991] edition.

Even if it had been made up, considering the amount of abuse the English language gets - it still conveys the correct meaning for most English speaking countries.
« Last Edit: 31 Jan 2020, 05:15:20 pm by ian »
Our desire for order and definition is often outweighed by our ability.

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1054
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Date: over a year, under a month, a few days etc
« Reply #19 on: 31 Jan 2020, 10:39:53 pm »
according to my Oxford English Dictionary [revised 1991] edition.
Odd given it's not in the '86... and Noah Webster says "No!" as does my threadbare 1898 Funk and Wagnalls. Not a great source for modern English, but fun for historical use!

Even if it had been made up, considering the amount of abuse the English language gets - it still conveys the correct meaning for most English speaking countries.
Because it's phonetically akilter, it failed to do so for me. I had to read the thread to figure out what was even being said; since if you use the lise form, it's supposed to be pronounced like the word "lease" or the name "Lisa"

NOT that such structural rules are obeyed in a consistent manner what with English being riddled with lend-words from dozens if not hundreds of languages, none of which follow the same construction rules.

Funny though, I'm usually good with anglicisms, my own speech is riddled with them... Hell most of the time I have to fight the urge to break into Olde Englisc.

But what's the old joke? The English and Americans are two people separate by a common language?

"Joy, we're back in Brooklyn now, It's not jew-ah, it's Joy. It's not jack-ay, it's Jackie. It's not Sade alright? It's Sadie!"
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Date: over a year, under a month, a few days etc
« Reply #20 on: 1 Feb 2020, 01:42:20 am »
** puts hand up **

I didn’t name or create this function... :lol:


Thanks again for the continued help - sorry for the constant posting!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

 

SMF spam blocked by CleanTalk

Advertisement