Solved

PHP change from days to day text

Posted on 2013-01-25
7
228 Views
Last Modified: 2013-01-25
Hi guys,

I have a variable called $remind_days I'm printing on a page.

When the variable is 1 I want the sentence I'm printing to read 1 day and when it's more than one day to read days (e.g. 3 days).

Here's the code for the line:

You have an active reminder for every ' . $remind_days.  ' days of inactivity

Open in new window

0
Comment
Question by:TLN_CANADA
  • 4
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 38820957
Try:
You have an active reminder for every ' . $remind_days.  ' day' . ((1 == $remind_days) ? '' : 's') . ' of inactivity

Open in new window

0
 

Author Closing Comment

by:TLN_CANADA
ID: 38821003
Thanks so much Robert!
0
 

Author Comment

by:TLN_CANADA
ID: 38821008
I also need to alter it slightly to do the same on this line, I'm not quite sure how to do this though.

$message = "Greetings $pieces[0], \n\n As you'd asked us before, we're reminding you that you have not logged any exercise in $days days.

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38821035
Since you're using double quotes there it should be:
$message = "Greetings $pieces[0], \n\n As you'd asked us before, we're reminding you that you have not logged any exercise in $days day" . ((1 == $days) ? '' : 's') . ".

Open in new window

I assume something else is following that otherwise don't forget to end the line with:
";

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38821047
Yes, that's correct. I assume this is correct now? It's a cron script so I can't check it.

$message = "Greetings $pieces[0], \n\n As you'd asked us before, we're reminding you that you have not logged any exercise in $days day" . ((1 == $days) ? '' : 's') . ". \n\n Hope to see you soon! \n\n";

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38821077
It looks ok but you should never assume. Of course you can test, just take the relevant pieces of the code and hard-code some values. Something like:
$username = "Robert Schutt";
$pieces = explode(" ", $username);
$days = 1;
$message = "Greetings $pieces[0], \n\n As you'd asked us before, we're reminding you that you have not logged any exercise in $days day" . ((1 == $days) ? '' : 's') . ". \n\n Hope to see you soon! \n\n";
echo $message;

Open in new window

and then test it with $days = 1 (the value being tested) and one or more other values.
0
 

Author Comment

by:TLN_CANADA
ID: 38821085
Works like a charm! Thank you very much mate!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now