Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Random Numbers and Dates.docx59.1 KB Random Numbers and Dates - Solution.docx
59.1 KB
Quick reference
Random Numbers and Dates
PHP makes creating random numbers and dates very easy.
When to use
Anytime you need to generate a random number or echo out a date, use these functions.
Instructions
PHP has many built in functions. Use them just like we called our own functions in the last video.
To create a random number between two numbers:
echo rand(1,10);
To create the date, say - the year:
echo date('Y');
To find a list of date parameters, check out the PHP documentation:
http://php.net/manual/en/function.date.php
Hints & tips
- PHP has many built in functions
- rand() and date();
- 00:04 In this video, we wanna talk about generation random numbers and
- 00:08 also using dates.
- 00:10 In last video, we talked about functions and I mentioned at the beginning of
- 00:13 the video that php has got like over build in functions and
- 00:16 we gonna look a couple of these right now.
- 00:18 We will look at a few more as we move on through the course.
- 00:20 But these are one of the great things about PHP.
- 00:23 They've done a lot of the stuff for you already and
- 00:25 you can just tap into these functions.
- 00:27 The first one we're gonna look at is random number generators and
- 00:30 there's lots of times when you need to generate a random number.
- 00:33 Later on in the course, we're gonna build a flashcard app and
- 00:36 we're gonna have to generate some random numbers.
- 00:38 So, that's just one reason why you might need random numbers.
- 00:41 With PHP, we use the rand function.
- 00:43 And its just RAND and parenthesis like we always do when we call a function and
- 00:49 if we echo this out onto the screen, save this come back here and
- 00:53 hit reload, we get this big huge random number.
- 00:56 So that's cool, but that's not all that useful, right?
- 00:59 Whenever you want a random number,
- 01:00 you generally want it within a certain range between one and ten.
- 01:04 Or one and 100.
- 01:04 In order to create a range, it's just like passing in parameters to
- 01:09 any other function, you just set it right here.
- 01:11 So one and ten would return a random number between one and ten.
- 01:15 So if we save this and hit reload, boom.
- 01:16 We get ten.
- 01:17 7, 9. I'm hitting reload on my keyboard here.
- 01:21 8, 5, 10, you get the idea.
- 01:23 If you wanted this between 1 and a 100, just change it to 100.
- 01:26 86, 62, just like that.
- 01:29 Pretty simple.
- 01:30 That's random numbers.
- 01:32 Now, we're going to talk about dates.
- 01:34 And there's lots of reasons why you might need a date.
- 01:37 Popular on webpages down at the bottom of the screen, it will say like
- 01:40 copyright 2018, 2017, whatever year that is all rights are reserved.
- 01:45 Well, most of the time that's done dynamically because you don't want to go
- 01:49 back every year and change the date on all your pages.
- 01:51 We can do this with PHP.
- 01:53 Again, we're gonna use one of PHP's built in functions, the date function.
- 01:56 Which is date( ) and our parenthesis, and we can output any sort of data,
- 02:02 we want, actually let's put echo and actually we get back on there.
- 02:06 Let's say we wanted to output the year,
- 02:07 we would just pass parameter of capital Y, and I'll explain that in just a minute.
- 02:12 But if we save this and hit reload, we see 2007 A team, so
- 02:15 there's all kinds of parameters you can pass the date function.
- 02:18 And in fact, if we go to Google and just type in php date, you will see there is
- 02:22 a PHP Manual, and this is a very important popular website.
- 02:27 It's a php.net, the official php website and they've got really good documentation
- 02:31 for all the different functions and all the different.
- 02:33 Things you could ever wanna know about PHP.
- 02:35 So I recommend you check that out.
- 02:37 But if we just look at this date documentation.
- 02:39 We see this chart, and it has all these letters.
- 02:42 Upper case and lower case.
- 02:44 These are things we can pass in to our date function.
- 02:47 Here's the day of the month with two digits, it's a d.
- 02:50 And to add these on, we just put a comma and then just pop them in there.
- 02:55 So if we save this, what did we say this was?
- 02:56 The Day of the month two digits, so
- 02:59 today's the 25th of the month so it should say 25.
- 03:04 And there's all kinds of things, its not just numbers,
- 03:08 you can also say like a capital D,
- 03:10 will give you the actual day of the week with three letters, lets check that out.
- 03:16 Save this, hit reload Tuesday.
- 03:18 Today is Tuesday so you can look through this chart and
- 03:20 you can see all of the different things.
- 03:22 You have the full month spelled out,
- 03:25 you could have the month as a numeric type of thing, you can have just three letters,
- 03:30 you could do the days, you could do the weeks.
- 03:32 All kinds of different things and its just as easy as Adding, whatever.
- 03:39 You just keep stringing these along in whatever sort of format you want
- 03:43 in order to do the date.
- 03:44 Those are two functions that you are going to use often with PHP.
- 03:48 Like I said, PHP has thousands of built-in functions.
- 03:51 You can go to this PHP.net Click on the documentation page and sort of read
- 03:55 through there if you want, or just Google a specific thing that you're looking for.
- 03:59 You're always gonna find the PHP.NET manual page for that thing.
- 04:03 In the next video, we're gonna talk about string manipulation.
Lesson notes are only available for subscribers.