'sample'에 해당되는 글 3건
- 2008.11.04 PHP - Date, Session, Cookies
- 2008.11.04 PHP - String Sample Code
- 2008.11.04 PHP - File Sample Code
PHP Date Function
<?php
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo "Tomorrow is ".date("m/d/y", $tomorrow);
?>
Important Full Date and Time:
- r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")
Time:
- a: am or pm depending on the time
- A: AM or PM depending on the time
- g: Hour without leading zeroes. Values are 1 through 12.
- G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
- h: Hour with leading zeroes. Values 01 through 12.
- H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
- i: Minute with leading zeroes. Values 00 through 59.
- s: Seconds with leading zeroes. Values 00 through 59.
Day:
- d: Day of the month with leading zeroes. Values are 01 through 31.
- j: Day of the month without leading zeroes. Values 1 through 31
- D: Day of the week abbreviations. Sun through Sat
- l: Day of the week. Values Sunday through Saturday
- w: Day of the week without leading zeroes. Values 0 through 6.
- z: Day of the year without leading zeroes. Values 0 through 365.
Month:
- m: Month number with leading zeroes. Values 01 through 12
- n: Month number without leading zeroes. Values 1 through 12
- M: Abbreviation for the month. Values Jan through Dec
- F: Normal month representation. Values January through December.
- t: The number of days in the month. Values 28 through 31.
Year:
- L: 1 if it's a leap year and 0 if it isn't.
- Y: A four digit year format
- y: A two digit year format. Values 00 through 99.
Other Formatting:
- U: The number of seconds since the Unix Epoch (January 1, 1970)
- O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
Starting a PHP Session
<?php session_start(); $_SESSION['views'] = 1; // store session data echo "Pageviews = ". $_SESSION['views']; //retrieve data ?>
<?php
session_start();
session_destroy();
?>
PHP Cookies - Background
<?php if(isset($_COOKIE['lastVisit'])) $visit = $_COOKIE['lastVisit']; else echo "You've got some stale cookies!"; echo "Your last visit was - ". $visit; ?>
Your last visit was - 11:48 - 02/28/08
PHP - String Position - strpos
$numberedString = "1234567890"; // 10 numbers from 1 to 0
$fivePos = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePos";
PHP str_replace Function
//string that needs to be customized $rawstring = "Welcome Birmingham parents. Your replaceme is a pleasure to have!"; //male string $malestr = str_replace("replaceme", "son", $rawstring); //female string $femalestr = str_replace("replaceme", "daughter", $rawstring); echo "Son: ". $malestr . "<br />"; echo "Daughter: ". $femalestr;
PHP substr_replace Function
//string that needs to be customized $original = "ABC123 Hello Mr. Cow! DEF321"; //starting point 5 $sp5 = substr_replace($original, "Five", 5); //starting point 12 $sp12 = substr_replace($original, "Twelve", 12); //starting point 0 $sp0 = substr_replace($original, "Zero", 0); //starting point -1 $spneg1 = substr_replace($original, "Negative 1", -1); //Echo each string echo "Original String: $original <br />"; echo "Starting Point 5: $sp5 <br />"; echo "Starting Point 12: $sp12 <br />"; echo "Starting Point 0: $sp0 <br />"; echo "Starting Point -1: $spneg1 ";
PHP - String Capitalization Functions
$originalString = "String Capitalization 1234";
$upperCase = strtoupper($originalString);
echo "Old string - $originalString <br />";
echo "New String - $upperCase";
PHP - String Explode
$rawPhoneNumber = "800-555-5555"; $phoneChunks = explode("-", $rawPhoneNumber); echo "Raw Phone Number = $rawPhoneNumber <br />"; echo "First chunk = $phoneChunks[0]<br />"; echo "Second chunk = $phoneChunks[1]<br />"; echo "Third Chunk chunk = $phoneChunks[2]";
PHP - Array implode
$pieces = array("Hello", "World,", "I", "am", "Here!"); $gluedTogetherSpaces = implode(" ", $pieces); $gluedTogetherDashes = implode("-", $pieces); for($i = 0; $i < count($pieces); $i++){ echo "Piece #$i = $pieces[$i] <br />"; } echo "Glued with Spaces = $gluedTogetherSpaces <br />"; echo "Glued with Dashes = $gluedTogetherDashes";
Piece #0 = Hello
Piece #1 = World,
Piece #2 = I
Piece #3 = am
Piece #4 = Here!
Glued with Spaces = Hello World, I am Here!
Glued with Dashes = Hello-World,-I-am-Here!
PHP - File Create
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
PHP - File Open
$ourFileName = "testFile.txt"; $fh = fopen($ourFileName, 'X') or die("Can't open file"); fclose($fh);
PHP - File Close
$ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle);
PHP - File Write
$myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Bobby Bopper\n"; fwrite($fh, $stringData); $stringData = "Tracy Tanner\n"; fwrite($fh, $stringData); fclose($fh);
PHP - File Read
$myFile = "testFile.txt"; $fh = fopen($myFile, 'r');
PHP - File Delete
$myFile = "testFile.txt"; unlink($myFile);
PHP - File Append
$myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh);
PHP - File Truncate
$myFile = "testFile.txt"; $fh = fopen($myFile, 'w'); fclose($fh);
PHP - File Upload
<form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form>
// Where the file is going to be placed $target_path = "uploads/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);$target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; }