0% found this document useful (0 votes)
60 views11 pages

String Functions in PHP

This document discusses various string, file, and date functions in PHP. It provides examples of string functions like strlen(), strrev(), explode(), and implode(). It also demonstrates file handling functions such as file_put_contents() and file_get_contents(). Finally, it shows examples of date functions like date_create(), date_diff(), and format() to output dates and time differences in various formats.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
60 views11 pages

String Functions in PHP

This document discusses various string, file, and date functions in PHP. It provides examples of string functions like strlen(), strrev(), explode(), and implode(). It also demonstrates file handling functions such as file_put_contents() and file_get_contents(). Finally, it shows examples of date functions like date_create(), date_diff(), and format() to output dates and time differences in various formats.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 11

String, File and Date

Functions in PHP
string functions in PHP
• strlen ()
• strrev()
• str_word_count()
• strtoupper()
• strtolower()
• implode()
• explode()
• substr(string,start,length)
• <p> implode function - array to a string </p>
• <?php
• $arr = array('Java','C++','CSS','VB');
• echo implode(" ",$arr);
• ?>
• <p> Explode function - string to an array </p>
• <?php
• $str = "Hello world. It's a beautiful day.";
• print_r (explode(" ",$str));
• ?>
File handling
• file_put_contents(filename, data, mode, context)
• - filename and data are compulsary and remaining
parameters are optional.
• file_put_contents("test.txt","Hello World - IWP.
Testing!");

• Return value -> The number of bytes written into the file on
success, FALSE on failure
• file_get_contents function in php reads the file
content into a string.
• file_get_contents(path, include_path, context, star
t, max_length) -> path compulsory and remaining
parameters are optional.
• echo file_get_contents("test.txt");
Date Functions – oops in php
<?php
$l = date_create($_GET["birthday"]); // read from form input type is date
from html using GET method.
$d = date_create("2022-04-30");// date_create() returns DateTime object
$df = date_diff($l,$d); // date_diff()// returns the difference between two DateTime
object as DateInterval object

echo $df->format("%R%a days"); // oops concept – format method of


DateInterval object to the required output
?>
• <?php
• $l = date_create($_GET["birthday"]);
• $d = date_create("2022-04-30");
• $df = date_diff($l,$d);

• echo $df->format("%R%a days");


• echo $df->format("%m-%y-%d");
• ?>
date() and time() functions
• <!DOCTYPE html>
• <html>
• <body>

• <?php
• $t=time();//The time() function returns the current time in the number of seconds since the January 1 1970 00:00:00 GMT

• echo($t . "<br>");
• echo(date("Y-m-d",$t));// date() returns the date in the year-month-date format from $t.
• ?>

• </body>
• </html>
Format method
• DateInterval::format(format $string): String -> (will be discussed in
detail while discussing functions in php)

• format method takes every format parameter using % symbol

• Next slide has the list of characters for getting the output as per our
requirement.
Required. Specifies the format. The following characters can be used in
the format parameter string:% - Literal %
•Y - Year, at least 2 digits with leading zero (e.g 03)
•y - Year (e.g 3)
•M - Month, with leading zero (e.g 06)
•m - Month (e.g 6)
•D - Day, with leading zero (e.g 09)
•d - Day (e.g 9)
•a - Total number of days as a result of date_diff()
•H - Hours, with leading zero (e.g 08, 23)
•h - Hours (e.g 8, 23)
•I - Minutes, with leading zero (e.g 08, 23)
•i - Minutes (e.g 8, 23)
•S - Seconds, with leading zero (e.g 08, 23)
•s - Seconds (e.g 8, 23)
•F - Microseconds, at least 6 digits (e.g 004403, 235689)
•f - Microseconds (e.g 4403, 235689)
•R - Sign "-" when negative, "+" when positive
•r - Sign "-" when negative, empty when positive
•Note: Each format character must be prefixed by a % sign!
Reference
• W3.schools.com

You might also like