Jump to content

[SOLVED] Learning Functions. Don't understand example.


foo

Recommended Posts

echo date('j F Y');

 

Ok, I think I understand what is happening above. It says print this date in this particular format with day, month, year in that particular order.

 

 

But now the book Sams Teach Yourself PHP in 10 minutes a day shows the following example for its explanation.

string date (string format [, int timestamp])

 

I'd quote the book but I don't know if that's ok to do so.

I don't understand what is happening with this particular function. Maybe this is a bad example for the book to begin with to explain how functions work.

date is a string object I take it? And inside this object you can have something maybe? I am not sure what though, is it just one thing, is it two things?

It really looks as if it's saying format something that's inside of the [ ] to a string format. I am guessing because it looks like there maybe a different format inside the [ ]. But, but there's a comma all by its lonesome on the left hand-side inside the brackets. What in the world happened there?

 

Thanks in advance for any info.

 

 

Link to comment
Share on other sites

string date (string format [, int timestamp])

 

The above line simply tells you the syntax of a given function; what does the function return, what arguments does it take and what type are those arguments.

 

string date means that the function date() returns a string (mixture of numbers and letters)

string format means that the function takes an argument called 'format' and that it has to be a string

[, int timestamp] means that the function takes an optional parameter called 'timestamp' and that the timestamp should be an int (only numbers)

 

That's used throughout the PHP manual found at www.php.net/manual :)

Link to comment
Share on other sites

string date (string format [, int timestamp])

 

The above line simply tells you the syntax of a given function; what does the function return, what arguments does it take and what type are those arguments.

 

string date means that the function date() returns a string (mixture of numbers and letters)

Ok, so when I see date() I see that the () means that it could return something.

string format means that the function takes an argument called 'format' and that it has to be a string

Argument called format, how do I see this argument to know what it does? I am guessing it's a function on its own that changes what it returns as well, yes?

http://www.php.net/date doesn't show the inner workings of format. Is this something I can just disregard? Do I need to know what the format function looks like?

[, int timestamp] means that the function takes an optional parameter called 'timestamp' and that the timestamp should be an int (only numbers)

I don't understand this. At the manual's page: http://www.php.net/date it says that format itself is a parameter. Where does timestamp come from?

That's used throughout the PHP manual found at www.php.net/manual :)

Link to comment
Share on other sites

Functions take parameters. The parameters allow a function to know what it is supposed to do. Most functions return a value. In the case of the date() function, it returns a formatted string of a given date.

 

To be able to return this date, it needs to know two things. Firstly, it needs to know what format it should return the date in. Second, it needs to know what date and time it is returning a format for.

 

Its first parameter, format, is the format of the returned value. This parameter is a string - as was said, a string is a 'line of characters' if you will. In this particular case, the string is composed of specific letters, which tell the function what format it is. For example, the character 'd' is used to display the day of the month (full list of chracters here.

 

The second parameter is the date and time that we are returning a function for. The reason why it is optional is the function can assume that we want to use the current date and time. So, if we dont pass a value for the second parameter, the function returns a formatted string representing the current date and time. However, if we do pass a value, it must be an integer (thats what the int represents in the above function prototype(thats whats this thing - string date (string format [, int timestamp]) - is called)). That integer is the number of seconds passed since the unix epoch (1st january 1970).

 

Hopefully that clears some of that up.

Link to comment
Share on other sites

Thank you. That answered a lot for me.

 

By the way, I love your music playlist php script. Very cool indeed.

 

Ok so let me run through some of the examples to make sure I get this:

 

echo date("l");

 

print the date(/*return in the format of l*/ "l"); //which is how we format the date if we want to know the day of the week.

//l = A full textual representation of the day of the week = Sunday through Saturday

/*we didn't supply a specific date/timestamp so because of the optional, the code chooses our current date and inputs into the paramater area which is denoted with [ ] because that means this is an optional parameter placement for the function.*/

#So in that example we only formatted today's date and printed it.

 

(Not tested)

So, if I want the day of the month:

echo date("d");

If I wanted the day and the week:

echo date("d,W");

Day, Week, Month:

echo date("d,W, F");

 

Seem right?

 

Ok, so those are when we don't supply a particular date.

But what about when we do. Is this an example of that?

// Prints: July 1, 2000 is on a Saturday

echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

 

Why does this example include mktime instead of just being:

// Prints: July 1, 2000 is on a Saturday

echo "July 1, 2000 is on a " . date("l", (7, 1, 2000));

 

I am still confused by the comma in:

string date ( string $format [, int $timestamp] )

See how the comma is inside the optional but in the previous example the comma is outside the optional?

 

Thanks so much for taking the time out to explain these features of the language. It is very interesting and mind boggling at the same time.

Link to comment
Share on other sites

So, if I want the day of the month:

echo date("d");

If I wanted the day and the week:

echo date("d,W");

Day, Week, Month:

echo date("d,W, F");

 

Seem right?

 

Yeah, thats right. In these examples, the output would be with a comma between each part of the date.

 

Ok, so those are when we don't supply a particular date.

But what about when we do. Is this an example of that?

// Prints: July 1, 2000 is on a Saturday

echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

 

Why does this example include mktime instead of just being:

// Prints: July 1, 2000 is on a Saturday

echo "July 1, 2000 is on a " . date("l", (7, 1, 2000));

 

Yes, that is indeed an example of that. The reason why you need to use the mktime function is that the second parameter for the date() function is an integer. The mktime function takes the various elements that compose a date and time, and returns an integer which is the number of seconds since unix epoch.

 

Your second example would actually return an error. As is, it would find an unexpected comma after the 7. If you quoted the date, it would return an error stating that the second parameter of date() is not the expected form.

 

I am still confused by the comma in:

string date ( string $format [, int $timestamp] )

 

This is merely the convention that is followed in the prototype of the function. It actually makes sense if you think of anything being inside the square brackets as optional - if the comma were outside, then that would imply that you would use the comma without specifying the timestamp, which, of course, you dont. You say that in the previous example, the comma is outside - that's not the case. Its simply that when using the function, there is nothing to show which parameters are optional. You simply use a comma to separate all arguments.

 

By the way, I love your music playlist php script. Very cool indeed.

 

Thank you. Its not too complex - it grabs the track listing from my last.fm webpage (which is generated from an application which monitors what i play through WMP), and creates the image with that text on it.

 

 

 

Link to comment
Share on other sites

Ok, so we can use this function to get the time and date and then display it in a format that we want specifically.

That's the default without using any optional time/date.

Then this same date function gives us an added functionality that if we wanted to, we could also find an amount of time between dates/times by including parameters into the optional part.

 

int mktime  ([ int $hour  [, int $minute  [, int $second  [, int $month  [, int $day  [, int $year  [, int $is_dst  ]]]]]]] )

 

Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

 

There might be more uses, but that's what I see for now and that might be good enough to get me going again.

 

I can almost see how the commas are coming into play after parameters and before optional elements if there are any, to separate those optional elements from the original parameter.

 

Thanks so much for the thorough explanations. I appreciate them very much. I wanted to make sure I had a better understanding before going any further with the book.

 

On to the next chapter. =)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.