Solved

PHP change from days to day text

Posted on 2013-01-25
7
231 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 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