You are on page 1of 67

Web Development

ADEL AHMED ALHAJJ


PHP Date and Time 2

 The date/time functions allow you to get the date and time from the server where your

PHP script runs.

 You can then use the date/time functions to format the date and time in several ways.

.‫ النصي‬PHP ‫ الوقت الحصول على التاريخ والوقت من الخادم حيث يتم تشغيل برنامج‬/ ‫تتيح لك دوال التاريخ‬ 

.‫ الوقت لتنسيق التاريخ والوقت بعدة طرق‬/ ‫يمكنك بعد ذلك استخدام دوال التاريخ‬ 

 The date() function formats a local date and time, and returns the formatted date

string.

.‫ وإرجاع سلسلة التاريخ المنسقة‬، ‫ على تنسيق التاريخ والوقت المحليين‬date () ‫تعمل الدالة‬ 
PHP Date and Time 3

 The date/time functions allow you to get the date and time from the server where your

PHP script runs.

 You can then use the date/time functions to format the date and time in several ways.

.‫ النصي‬PHP ‫ الوقت الحصول على التاريخ والوقت من الخادم حيث يتم تشغيل برنامج‬/ ‫تتيح لك دوال التاريخ‬ 

.‫ الوقت لتنسيق التاريخ والوقت بعدة طرق‬/ ‫يمكنك بعد ذلك استخدام دوال التاريخ‬ 

 The date() function formats a local date and time, and returns the formatted date

string.

.‫ وإرجاع سلسلة التاريخ المنسقة‬، ‫ على تنسيق التاريخ والوقت المحليين‬date () ‫تعمل الدالة‬ 
PHP Date and Time 4

 Here are some of the most common date format characters and their values.

Character Meaning Example


D day of the month with leading zeros 03 or 17

J day of the month without leading zeros 3 or 17

D day of the week as a three-letter abbreviation Mon


l full day of the week Monday
m month as a number with leading zeros 09 or 12

n month as a number without leading zeros 9 or 12

M month as a three-letter abbreviation Sep


F full month September
y two-digit year 18
Y full year 2018
PHP Date and Time 5

<?php

echo date('Y');
// Output — 2018

echo date('F Y');


// Output — September 2018

echo date('d F, Y');


// Output — 13 September, 2018

echo date('d F, Y (l)');


// Output — 13 September, 2018 (Thursday) ?>
PHP Date and Time 6

 You can also use the date() function to output the time. Here are some of the most

commonly used time format characters:

Character Meaning Example

g hours in 12-hour format without leading zeros 1 or 12

h hours in 12-hour format with leading zeros 01 or 12

G hours in 24-hour format without leading zeros 1 or 13

H hours in 24-hour format with leading zeros 01 or 13

a am/pm in lowercase am
A am/pm in uppercase AM
i minutes with leading zeros 09 or 15
s seconds with leading zeros 05 or 30
PHP Date and Time 7

<?php
echo date('h:i:s A');
// Output — 11:03:37 AM

echo date('l, h:i:s A');


// Output — Thursday, 11:04:09 AM

echo date('d F Y, h:i:s A');


// Output — 13 September 2018, 11:05:00 AM
?>
PHP Date and Time 8
PHP Date and Time 9
PHP Date and Time 10

 Convert a Datetime String to a Timestamp

 The strtotime() function will be helpful when you want to convert different date and

time values in string format to a timestamp.

 The function can parse almost all kinds of datetime strings into timestamps.

‫ مفيدة عندما تريد تحويل قيم مختلفة للتاريخ والوقت بتنسيق سلسلة إلى طابع‬strtotime() ‫ستكونالدالة‬ 

.‫زمني‬

.‫يمكن للوظيفة تحليل جميع أنواع سالسل التاريخ والوقت تقريبًا إلى طوابع زمنية‬ 
PHP Date and Time 11
PHP Date and Time 12

<?php
$some_time = strtotime("10 months 15 days 10 hours ago");
echo 'It was '.date('l', $some_time).' on '.date('d F, Y h:i:s', $some_time).'.';
// Output — It was Sunday on 29 October, 2017 03:16:46.

$some_time = strtotime("next month");


echo 'It is '.date('l', $some_time).' on '.date('d F, Y h:i:s', $some_time).'.';
// Output — It is Saturday on 13 October, 2018 01:18:05.
PHP Date and Time 13

$some_time = strtotime("third monday");


echo 'Date on the third monday from now will be '.date('d F, Y', $some_time).'.';
// Output — Date on the third monday from now will be 01 October, 2018.

$some_time = strtotime("last day of November 2021");


echo 'Last day of November 2021 will be '.date('l', $some_time).'.';
// Output — Last day of November 2021 will be Tuesday.

?>
PHP Date and Time 14
PHP Date and Time 15
PHP Date and Time 16
PHP Date and Time 17
PHP Array 18

 An array is a special variable, which can hold more than one value at a time.

 If you have a list of items (a list of car names, for example), storing the cars in single

variables could look like this:

$cars1 = "Volvo"; $cars2 = "BMW"; $cars3 = "Toyota";

 An array can hold many values under a single name, and you can access the values by

referring to an index number.


PHP Array 19

 Create an Array in PHP

 In PHP, the array() function is used to create an array:

 Type of Arrays

 In PHP, there are tow types of arrays:

 Indexed arrays - Arrays with a numeric index

 Associative arrays - Arrays with named keys


PHP Array 20

 PHP Indexed Arrays

 There are two ways to create indexed arrays:

 The index can be assigned automatically (index always starts at 0), like this:

$cars = array ("Volvo", "BMW", "Toyota") ;

 Or the index can be assigned manually:

$cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota";


PHP Array 21

 Get The Length of an Array

 The count() function is used to return the length (the number of elements) of an array:

<?php

$cars = array("Volvo", "BMW", "Toyota"); <?php

echo count($cars); $cars = array("Volvo", "BMW", "Toyota");

?> $arrlength = count($cars);

for ($x = 0; $x < $arrlength; $x++)

{ echo $cars[$x];

echo "<br>"; } ?>


PHP Array 22

 PHP Associative Arrays

 Associative arrays are arrays that use named keys that you assign to them.

 There are two ways to create an associative array:

$age = array("Ali"=>"35", "Sameer"=>"37", "Mohammed"=>"20");

 Or:

$age['Ali'] = "35"; $age['Mohammed'] = "37"; $age['Sameer'] = "20";


PHP Array 23

<?php

$age = array("Ali"=>"35", "Mohammed"=>"37", "Sameer"=>"20");

echo "Ali is " . $age['Ali'] . " years old.";

?>

====================================================

<?php

$age = array ("Ali"=>"35", "Mohammed"=>"37", "Sameer"=>"20");

foreach ($age as $x => $x_value)

{ echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; }

?>
PHP Array 24
PHP Array 25
PHP Array 26
PHP Array 27
PHP Array 28
PHP Array 29
PHP Array 30
PHP Array 31
Functions of Array >>> array_change_key_case() 32

 PHP array_change_key_case() function changes the case of all key of an array.

 array_change_key_case() Function is used to convert all the keys to the Uppercase or

Lowercase of an Array. >>> Note: It changes case of key only.

 Syntax

array_change_key_case(array, case);

 array – This parameter takes an array.

 case – This parameter takes the case option –

CASE_UPPER – Changes the keys to Uppercase

CASE_LOWER – Changes the keys to Lowercase


Functions of Array >>> array_change_key_case() 33
Functions of Array >>> array_chunk() 34

 The array_chunk() function is used to split an array into arrays with size elements.

 The last chunk may contain less than size elements.

 Syntax

array_chunk ( array, size, preserve_keys);

 array : Required. Specifies the array to use

 size : Required. An integer that specifies the size of each chunk

 preserve_keys : Optional. Possible values:

true - Preserves the keys

false - Default. Reindexes the chunk numerically


Functions of Array >>> array_chunk() 35

<?php

$input_array = array('name1', 'name2', 'name3', 'name4', 'name5');

print_r ( array_chunk ( $input_array , 2) ) ; ?>


Functions of Array >>> array_chunk() 36
Functions of Array >>> sort() 37

 The sort() function is used to sort array elements.

 Elements will be arranged from lowest to highest when this function has completed.

<?php

$subject=array("Language", "English", "Math", "History");

sort($subject);

foreach ($subject as $key => $val)

echo "subject[ " . $key . "] = " . $val . "<br />";

} ?>
Functions of Array >>> sort() 38
Functions of Array >>> sort() 39
Functions of Array >>> sort() 40

 sort() - sort arrays in ascending order

 rsort() - sort arrays in descending order

 asort() - sort associative arrays in ascending order, according to the value

 ksort() - sort associative arrays in ascending order, according to the key

 arsort() - sort associative arrays in descending order, according to the value

 krsort() - sort associative arrays in descending order, according to the key


Functions of Array >>> array_reverse() 41

 The array_reverse() function is used to reverse the order of the elements in an array.

 Syntax:

array_reverse(array , preserve_keys) ;

 array :Required. Specifies an array

 preserve :Optional. Specifies if the function should preserve the keys of the array or

not. Possible values:

 true

 false

 The default value is FALSE


Functions of Array >>> array_reverse() 42
Functions of Array >>> array_search() 43

 The array_search() function searches the specified value in an array.

 It returns key if search is successful.

 Syntax

array_search( value, array )

 Parameter Values

 value : Required. Specifies the value to search for

 array : Required. Specifies the array to search in


Functions of Array >>> array_search() 44

?php

$fruits = array( 0 => 'Orange', 1=> 'Apple', 2 => 'Banana', 3 => 'Cherry');

$result = array_search ('Cherry', $fruits) ;

echo $result; ?>


Functions of Array >>> array_search() 45
Functions of Array >>> array_merge() 46

 The array_merge() function merges one or more arrays into one array

 Syntax:

array_merge(array_name1, array_name2, array_name3...)

 Parameters:

 array_name1: The name of the array.

 array_name2: The name of the array.


Functions of Array >>> array_merge() 47
Functions of Array 48

 The array_pop() function is used to remove the last element of an array.

 Syntax: array_pop (array_name)

 The array_shift() function is used to remove the first element from an array,

 Syntax: array_shift(array_name)

 The array_splice() function removes selected elements from an array and replaces it

with new elements.

 The function also returns an array with the removed elements.


Functions of Array 49

 Syntax: array_splice (array, start, length, replacement)

 array (Required) – The array whose fraction of elements you want to remove.

 start (Required) – This parameter takes the position number of the element from where

you want to start removing.

 length (Optional) – The length defines how many elements you want to remove from the

starting number. If length is omitted, removes everything from starting position to the

end of the array

 replacement (Optional) – If you specify an array into this parameter, then the removed

elements will be replaced with elements from this array.


Functions of Array >>> array_pop() 50

?php

$array1= array ('Mathematics','Physics','Chemistry','Biology') ;

$subject = array_pop ($array1) ;

print_r($array1) ; ?>
Functions of Array >>> array_shift() 51

?php

$fruits_list = array(0 => 'Orange', 1=> 'Apple', 2 => 'Banana',3 => 'Cherry');

$result= array_shift ($fruits_list) ;

print_r($fruits_list) ;

echo '</br>'.$result;

?>
Functions of Array 52
Functions of Array >>> array_splice() 53

?php

$colors = array("red", "green", "blue", "yellow", "orange");

$removedEl = array_splice ($colors, 1, 2 );

print_r($colors);

echo "\n# - Removed Element\n";

print_r ($removedEl); ?>


Functions of Array >>> array_splice() 54

?php

$colors = array("red", "green", "blue", "yellow", "orange");

$newColors = array("purplle", "pink", "white");

$removedEl = array_splice ($colors, 1, 2, $newColors );

print_r($colors); echo "\n# - Removed Element\n";

print_r($removedEl); ?>
Functions of Array >>> array_splice() 55
Functions of Array >>> array_splice() 56
Functions of Array >>> array_splice() 57
Functions of Array >>> array_push() 58

 The array_push() function is used to add one or more elements onto the end of an

array. The length of array increases by the number of variables pushed.

 Syntax: array_push(array_name, value1, value2...)

<?php

$array1= array('Mathematics','Physics');

array_push ($array1,'Chemistry','Biology');

print_r($array1); ?>
Functions of Array >>> array_push() 59
Functions of Array >>> implode() 60

 The implode() function returns a string from the elements of an array.

 Syntax

Implode ( separator , array ) ;

 separator :Optional. Specifies what to put between the array elements.

 array :Required. The array to join to a string


Functions of Array >>> implode() 61
Functions of Array >>> implode() 62
Functions of Array >>> explode() 63

 The explode() function breaks a string into an array.

 Syntax

explode( separator, string , limit )

 separator Required. Specifies where to break the string

 string Required. The string to split

 limit Optional. Specifies the number of array elements to return.


Functions of Array >>> explode() 64
Functions of Array >>> explode() 65
Functions of Array >>> explode() 66

You might also like