The date() function converts a given timestamp into a readable date format.
Here is the syntax for this function:
Syntax
[code lang=”php”]
date(format,timestamp);
[/code]
Parameters:
format
This parameter determines the format and how the date will read.
For example: “Saturday, Nov. 16th 2009” would be the following code:
[code lang=”php”]
$timestamp = time();
date(“l, M. jS Y”,$timestamp);
[/code]
timestamp
This parameter is the timestamp you want to use to format a date from. This timestamp is a number of seconds since a certain date, since we used the current timestamp of time() we are making the date for the current time.
We’ll break down our example to see what each set of letters represents as part of the date’s format.
l – day of the week, textual, long; i.e. ‘Friday’
M – month, textual, 3 letters; i.e. ‘Jan’
j – day of the month without leading zeros; i.e. ’1′ to ’31′
S – English ordinal suffix, textual, 2 characters; i.e. ‘th’,‘nd’
Here is a table for formatting the PHP function date() and the letters used to represent different parts of a timestamp.
a
‘am’ or ‘pm’
A
‘AM’ or ‘PM’
B
Swatch Internet time
d
day of the month, 2 digits with leading zeros; i.e. ’01’ to
’31’
D
day of the week, textual, 3 letters; i.e. ‘Fri’
F
month, textual, long; i.e. ‘January’
g
hour, 12-hour format without leading zeros; i.e. ‘1’ to ’12’
G
hour, 24-hour format without leading zeros; i.e. ‘0’ to ’23’
h
hour, 12-hour format; i.e. ’01’ to ’12’
H
hour, 24-hour format; i.e. ’00’ to ’23’
i
minutes; i.e. ’00’ to ’59’
I (capital i)
‘1’ if Daylight Savings Time, ‘0’ otherwise.
j
day of the month without leading zeros; i.e. ‘1’ to ’31’
l (lowercase ‘L’)
day of the week, textual, long; i.e. ‘Friday’
L
boolean for whether it is a leap year; i.e. ‘0’ or ‘1’
m
month; i.e. ’01’ to ’12’
M
month, textual, 3 letters; i.e. ‘Jan’
n
month without leading zeros; i.e. ‘1’ to ’12’
r
RFC 822 formatted date; i.e. ‘Thu, 21 Dec 2000 16:01:07 +0200’
(added in PHP 4.0.4)
s
seconds; i.e. ’00’ to ’59’
S
English ordinal suffix, textual, 2 characters; i.e. ‘th’,
‘nd’
t
number of days in the given month; i.e. ’28’ to ’31’
T
Timezone setting of this machine; i.e. ‘MDT’
U
seconds since the epoch
w
day of the week, numeric, i.e. ‘0’ (Sunday) to ‘6’ (Saturday)
Y
year, 4 digits; i.e. ‘1999’
y
year, 2 digits; i.e. ’99’
z
day of the year; i.e. ‘0’ to ‘365’
Z
timezone offset in seconds (i.e. ‘-43200’ to ‘43200’). The
offset for timezones west of UTC is always negative, and for those east
of UTC is always positive.
Many times when you upload a image somewhere you want to resize it to different dimensions based off of a maximum width or height. Here is a simple script that does this for you, using a HTML form and a PHP script. We start with the PHP script that will run if our $_GET[‘do’] is set to “upload.” Then we include the HTML form that has three inputs (max width, max height, image file).
Here is the PHP for our function – generate_resized_image() – which will take the given image file, create a new file on our server, and then copy the uploaded image to our local image file with the new width and height:
[codesyntax lang=”php” title=”Upload and Resize PHP Code”]
<?php
// index.php
function generate_resized_image(){
$max_dimension = 800; // Max new width or height, can not exceed this value.
$dir = “./images/”; // Directory to save resized image. (Include a trailing slash – /)
// Array of valid extensions.
$valid_exts = array(“jpg”,”jpeg”,”gif”,”png”);
// Select the extension from the file.
$ext = end(explode(“.”,strtolower(trim($_FILES[“image”][“name”]))));
// Check not larger than 175kb.
if($postvars[“image_size”] <= 179200){
// Check is valid extension.
if(in_array($ext,$valid_exts)){
if($ext == “jpg” || $ext == “jpeg”){
$image = imagecreatefromjpeg($postvars[“image_tmp”]);
}
else if($ext == “gif”){
$image = imagecreatefromgif($postvars[“image_tmp”]);
}
else if($ext == “png”){
$image = imagecreatefrompng($postvars[“image_tmp”]);
}
// Grab the width and height of the image.
list($width,$height) = getimagesize($postvars[“image_tmp”]);
// If the max width input is greater than max height we base the new image off of that, otherwise we
// use the max height input.
// We get the other dimension by multiplying the quotient of the new width or height divided by
// the old width or height.
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
// Create random 4 digit number for filename.
$rand = rand(1000,9999);
$filename = $dir.$rand.$postvars[“image”];
// Create image file with 100% quality.
imagejpeg($tmp,$filename,100);
return “<strong>Image Preview:</strong><br/>
<img src=\””.$filename.”\” border=\”0\” title=\”Resized Image Preview\” style=\”padding: 4px 0px 4px 0px;background-color:#e0e0e0\” /><br/>
Resized image successfully generated. <a href=\””.$filename.”\” target=\”_blank\” name=\”Download your resized image now!\”>Click here to download your image.</a>”;
imagedestroy($image);
imagedestroy($tmp);
} else {
return “File size too large. Max allowed file size is 175kb.”;
}
} else {
return “Invalid file type. You must upload an image file. (jpg, jpeg, gif, png).”;
}
}
?>
[/codesyntax]
This function you can define in your header PHP files along with other functions or where ever you would like, so long as it is defined before the call of the function itself.
Here is the HTML for our form to submit the image file and the new dimensions:
Okay, so now we have our PHP function defined, included and our HTML contains this form submitting the image file and its new dimensions. All we need now is a short code of PHP to check our GET values, particularly do for the value upload. This tells us the form was submitted and we can start checking it for values, and creating our re-sized image.
Here is the PHP which will appear below the HTML form to show our re-sized image once the form is submitted:
I decided to make a free easy to use favicon generating script that uses PHP. The script has a few features and requirements when uploading your image to be converted to a favicon. First the script checks for a few things such as file type, file size, if the directory is writable, and the specified dimensions. You can create a 16×16 icon or a 32×32 icon.
This PHP Favicon Generator Script first checks the form submitted for the following:
Valid file size. (Default: Max 75kb)
Valid file types. (Valid extensions are defaulted to image files)
Writable directory for favicons.
Then, once verifying these variables, we create a temporary image file on our server with the new dimensions gathered from the form (16×16 or 32×32). We then copy the uploaded image file’s contents over to the temporary file, while resizing it. Once the temporary image file has the new width, height, and copied graphical output, we can transfer this temporary data to a file on our server. Finally, we rename this image file to have a favicon file extension (.ico). Wallah!
So we have the function included in our header, and the form is presented by the code above. Now we just need to call the function when our GET value for “do” is set to “create” – which the form does for us.
// Call the generate favicon function and echo its returned value
$gen_favicon = generate_favicon();
echo $gen_favicon;
echo “Place your download instructions and anything else you want here to follow the download link after upload.”;
}
}
}
?>
[/codesyntax]
Also be sure to follow this PHP with the closing tags for HTML:
[code lang=”html4strict”]
</body>
</html>
[/code]
Be sure to include the HTML head tags in your HTML pages that use the favicon. These are given on the script’s index and on the demo.
Open a new text document in notepad. Paste the following code within the document:
[code lang=”htaccess”]
<FilesMatch “\.(?i:ico|gif|png|jpg|jpeg)$”>
Header set Content-Disposition attachment
</FilesMatch>
[/code]
Now save this file as “.htaccess” exactly like that. Place this file in the directory you are saving your .ico files to for download. This will force the access of these files (ico, png, gif, jpeg) to be a download only type.
Here is a very simple function to use to ordinalize numbers in PHP. This adds the place value suffix to numbers. So you can turn numbers like 1, 2, 3 into 1st, 2nd, 3rd.
Basically the function first checks if the number is in the range of 11-13, and if so it returns the number with “th” attached (11th, 12th, 13th). If it is not in this range it checks the remainder of the number divided by 10.
So let’s say our number was 34. 10 goes into 34 three times, with a remainder of 4. Now the function runs this value against three cases, those being 1, 2, and 3. Since it is not one of them, the default value is used, which is “th.” Thus returning “4th.”
Let’s say you want to have a simple HTML <select> form as a drop down for rows in a Mysql table. This could be for things like categories, pages, games, anything you want to have in a drop down to navigate to another page or submit a form. What ever the case is, I’m going to make a simple layout for displaying rows returned from a mysql query as <option>’s in a HTML <select> or drop down.
This will grab all the rows in “table1” ordered by the value of “id” descending. You can make this query whatever you want whether you want it ordered differently or with other requirements, etc. Now we will just make a <select> inside of a HTML form that holds each of these values as a option.
[code lang=”html”]
Categories
[/code]
If you have rows returned in you mysql query you will have a HTML drop down that looks like this:
This HTML form is being submitted to “index.php?do=nav.” Of course you can point this where ever you want to do whatever you want with it, but let’s say we want to have it direct you to a category with PHP. So we are going to run a function on index.php?do=nav that will redirect the viewer to the category.
The report_level parameter is optional and specifies what report level to report for the current script. This can be set by its numeric value or its constant name, however for future versions of PHP it is recommended you use the constant name rather than the numeric value.
Report Levels
Value
Constant
Description
1
E_ERROR
Fatal run-time errors. Errors that can not be recovered
from. Execution of the script is halted
2
E_WARNING
Non-fatal run-time errors. Execution of the script is not
halted
4
E_PARSE
Compile-time parse errors. Parse errors should only be
generated by the parser
8
E_NOTICE
Run-time notices. The script found something that might be
an error, but could also happen when running a script normally
16
E_CORE_ERROR
Fatal errors at PHP startup. This is like an E_ERROR in the
PHP core
32
E_CORE_WARNING
Non-fatal errors at PHP startup. This is like an E_WARNING
in the PHP core
64
E_COMPILE_ERROR
Fatal compile-time errors. This is like an E_ERROR
generated by the Zend Scripting Engine
128
E_COMPILE_WARNING
Non-fatal compile-time errors. This is like an E_WARNING
generated by the Zend Scripting Engine
256
E_USER_ERROR
Fatal user-generated error. This is like an E_ERROR set by
the programmer using the PHP function trigger_error()
512
E_USER_WARNING
Non-fatal user-generated warning. This is like an E_WARNING
set by the programmer using the PHP function trigger_error()
1024
E_USER_NOTICE
User-generated notice. This is like an E_NOTICE set by the
programmer using the PHP function trigger_error()
2048
E_STRICT
Run-time notices. PHP suggest changes to your code to help
interoperability and compatibility of the code
4096
E_RECOVERABLE_ERROR
Catchable fatal error. This is like an E_ERROR but can be
caught by a user defined handle (see also set_error_handler())
8191
E_ALL
All errors and warnings, except level E_STRICT (E_STRICT
will be part of E_ALL as of PHP 6.0)
Here is an example of the error_reporting function in PHP:
Drop down menus are very useful for navigation on websites and for holding many links to pages on your site without taking up much space on your web pages. Using just CSS and Javascript we can make a nice simple drop down menu you can put on your web page. To do this we’ll have three files: dropdown.css, dropdown.js, and index.html. Our index.html page will display the drop down menu and our stylesheet – dropdown.css – will hold the styles for the drop down menu.
Here is a preview of what our drop down menu will look like:
<body>
<ul id=”dropdown”>
<li><a href=”#”>Homepage</a></li>
<li><a href=”#” onmouseover=”mopen(‘list1’)” onmouseout=”mclosetime()”>Drop Down Menu #1</a>
<div id=”list1″ onmouseover=”mcancelclosetime()” onmouseout=”mclosetime()”>
<a href=”#”>Drop Down Link #1</a>
<a href=”#”>Drop Down Link #2</a>
<a href=”#”>Drop Down Link #3</a>
</div>
</li>
<li><a href=”#” onmouseover=”mopen(‘list2’)” onmouseout=”mclosetime()”>Drop Down Menu #2</a>
<div id=”list2″ onmouseover=”mcancelclosetime()” onmouseout=”mclosetime()”>
<a href=”#”>Drop Down Link #1</a>
<a href=”#”>Drop Down Link #2</a>
<a href=”#”>Drop Down Link #3</a>
</div>
</li>
<li><a href=”#”>Link #1</a></li>
<li><a href=”#”>Link #2</a></li>
<li><a href=”#” onmouseover=”mopen(‘list3’)” onmouseout=”mclosetime()”>Drop Down Menu #3</a>
<div id=”list3″ onmouseover=”mcancelclosetime()” onmouseout=”mclosetime()”>
<a href=”#”>Drop Down Link #1</a>
<a href=”#”>Drop Down Link #2</a>
<a href=”#”>Drop Down Link #3</a>
</div>
</li>
</ul>
</body>
</html>
[/codesyntax]
Here we’re going to take a look at variables inside and outside of PHP functions. It is very easy to make one error with a function and have it return the wrong value. We’re going to make a simple function named “func1” which we’ll tweak a bit to show how variables are used inside and out of functions in PHP.
Here is our function, take a look and see if you can tell what the output of the function will be:
[code lang=”php”]
[/code]
The output is nothing right now. haha So if you thought it was anything but nothing, that’s wrong. The function has no code in it whatsoever right now, and the variable “$var” is set to equal the returned value of the function “func1().” Since the function does nothing, $var is equal to nothing. Easy enough. Now let’s work with some variables.
[code lang=”php”]
[/code]
Now, we have a parameter to our PHP function. A parameter is any variable that is defined within the function’s parentheses. You can have many parameters in your functions – seperated by commas – but it is a good idea to keep your functions organized and simple to their purpose. So we have $var as a parameter to func1(). You can use these to submit values through a function and work with them inside the function to return a different or desired value. Right now, the function func1() is just returning $var. So whatever is put through as $var initially, will be returned back as it was.
So this code will result in the output:
hello
Let’s say we want to have a global variable we can use in several functions. Global variables are defined as such within a function as previously defined variables outside the function. In the following code we’ll define $name as “Brian” and use it in two functions.
[code lang=”php”]
“;
echo $goodbye;
?>
[/code]
The output of this code will be:
Hello Brian!
Goodbye Brian!
You can use global variables with PHP pages you include on other PHP pages. For example if you have variables defined on one PHP page, in this example “variables.php,” you can call that page to another and globally define variables in your functions to work with them and return what you like.
Variables.php:
[code lang=”php”]
[/code]
Now on our page we are working on, we’ll call it “display.php,” we’ll call the page variables.php and use these variables in functions.
In this post I am going to show you how to make a cool link bar for your website with Photoshop. Link bars are very important in websites as they provide a clear and easy way for viewers to reach other pages on your site. Basically we’ll make a background image which will be set to repeat along the x axis as the background of the cells in our table. Then the links in the link bar will be placed on top of the background image blending in nicely as the buttons on your link bar.
In CSS we make a few styles for the table that will serve as the link bar holder:
On our server we have to be sure to be placing the images all in the “images” folder in the “public_html” folder – your website’s default directory.
I made a new document with the dimensions 110×32 pixels as the size of a button. You can make them as big as you want for different words or a standard size, whatever you prefer.
1. Make a new layer and fill the layer with the color #EEEEEE as the background color of the link bar.
2. Make another layer and select a lower smaller half portion of the button to shade in with #E5E5E5.
3. Then make your text layer by typing whatever you want onto the canvas with the text tool on the tool panel. I started with “Home” as the first button. You want to apply the following settings to the text layer’s blending options. You can do this by double clicking on the text layer in the layers panel.
Double click the “Home” text layer and apply the settings below or something similar. You can change these grayscale colors to whatever you would like to fit your website’s color theme.
Our first button turns out something as so:
Now, you can make a rollover image if you would like. This will just make the image change when the viewer puts their cursor over it. We’ll add a slight glow on a new layer to make a rollover layer.
Optional Rollover Image Changes
4. Make a new layer. Select the brush tool in the tools panel. Select the 5th brush type, a glowy brush. Set the size to something like 32px and turn the opacity to 50%. Select the color white (#FFFFFF). Here are the settings:
5. At the top of the image drag half of the brush over the very top of the image and the other half off the canvas to make a slight white glow at the top of the image. Here is the result magnified:
Now as the background all we have to do is make a new document with dimensions 1×32. This will be a single line that will repeat as the background. Now do the same you did to the background of the buttons. Make a layer filled in as #EEEEEE and then another layer with 11 pixels or however much you selected along the bottom as #E5E5E5. Here is the background image close up:
Now as the seperator to the images we need to make a seperator image. This will be a very thin image that will sit between the buttons to give them some distinction.
6. Make a new document 2×32 pixels. On one layer copy the background image we made before and stretch it to be 2 pixels wide. (Press Ctrl+T to transform the layer). Make a new layer and fill the middle left half of the image with the color #DADADA and the middle right half with #F6F6F6. Here is the fill-in’s up close:
Now we have our links and our background image. You will need to make a rollover image for every button you want to have one. Now we just have to apply the CSS styles above to our table that will have the link bar images in it. We do this by setting the link tag in the <head> HTML tags.