PHP Date - Exercises, Practice, Solution
PHP date [28 exercises with solution]
1. Write a PHP script which will display the copyright information in the following format. To get current year you can use the date() function. Go to the editor
Expected Format : © 2013 PHP Exercises - w3resource
Click me to see the solution
2. Create a simple 'birthday countdown' script, the script will count the number of days between current day and birthday. Go to the editor
Click me to see the solution
3. Write a PHP script to print the current date in the following format. To get current date's information you can use the date() function. Go to the editor
Sample format : (assume current date is September 01, 2013)
2013/09/01
13.09.01
01-09-13
Click me to see the solution
4. Write a PHP script to calculate the difference between two dates. Go to the editor
Sample dates : 1981-11-04, 2013-09-04
Expected Result : 31 years, 10 months, 11 days
Click me to see the solution
5. Write a PHP script to convert a date from yyyy-mm-dd to dd-mm-yyyy. Go to the editor
Sample date : 2012-09-12
Expected Result : 12-09-2012
Click me to see the solution
6. Write a PHP script to convert the date to timestamp. Go to the editor
Sample date : 12-05-2014
Expected Result : 1399852800
7. Write a PHP script to calculate a number of days between two dates. Go to the editor
Click me to see the solution
8. Write a PHP script to get the first and last day of a month from a specified date. Go to the editor
Click me to see the solution
9. Write a PHP script to print like : Saturday the 7th. Go to the editor
Click me to see the solution
10. Write a PHP script to check whether the given dates are valid or not? Go to the editor
Click me to see the solution
11. Write a PHP script to get time difference in days and years, months, days, hours, minutes, seconds between two dates. Go to the editor
Note : Use DateTime class.
Click me to see the solution
12. Write a PHP script to change month number to month name. Go to the editor
Click me to see the solution
13. Write a PHP script to get yesterday's date. Go to the editor
Click me to see the solution
14. Write a PHP script to get the current date/time of 'Australia/Melbourne'. Go to the editor
Click me to see the solution
15. Write a PHP script to check whether a date is a weekend or not. Go to the editor
Click me to see the solution
16. Write a PHP script to add/subtract the number of days from a particular date. Go to the editor
Sample Output : Original date : 2011-01-01
Before 40 days : 2010-11-22
After 40 days : 2011-02-10
Click me to see the solution
17. Write a PHP function to get start and end date of a week (by week number) of a particular year. Go to the editor
Sample week and year : 12, 2014
Output :
Starting date of the week: 2014-3-24
End date the week: 2014-3-30
Click me to see the solution
18. Write a PHP script to calculate the current age of a person. Go to the editor
Sample date of birth : 11.4.1987
Output : Your age : 27 years, 1 month, 29 days
Click me to see the solution
19. Write a PHP script to calculate weeks between two dates. Go to the editor
Sample Output : Weeks between 1/1/2014 and 12/31/2014 is 52
Click me to see the solution
20. Write a PHP script to get the number of the month before the current month. Go to the editor
Click me to see the solution
21. Write a PHP script to convert seconds into days, hours, minutes and seconds. Go to the editor
Sample seconds : 200000
Expected output : 2 days, 7 hours, 33 minutes and 20 second
Click me to see the solution
22. Write a PHP script to get the last 6 months from the current month. Go to the editor
Click me to see the solution
23. Write a PHP script to get the current month and previous three months. Go to the editor
Click me to see the solution
24. Write a PHP script to increment date by one month Go to the editor
Sample date : 2012-12-21
Expected Output : 2013-01-21
Click me to see the solution
25. Write a PHP script to get the current date in Italian. Go to the editor
Sample Output : Today is lun on ott 06, 2014
Click me to see the solution
26. Write a PHP script to convert the number to month name. Go to the editor
Click me to see the solution
27. Write a PHP script to get the number of days of the current month. Go to the editor
Click me to see the solution
28. Write a PHP script to display time in a specified timezone. Go to the editor
Click me to see the solution
PHP Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
PHP: Tips of the Day
Members of objects or classes can be accessed using the object operator (->) and the class operator (::).
Example:
class MyClass { public $a = 1; public static $b = 2; const C = 3; public function d() { return 4; } public static function e() { return 5; } } $object = new MyClass(); var_dump($object->a); // int(1) var_dump($object::$b); // int(2) var_dump($object::C); // int(3) var_dump(MyClass::$b); // int(2) var_dump(MyClass::C); // int(3) var_dump($object->d()); // int(4) var_dump($object::d()); // int(4) var_dump(MyClass::e()); // int(5) $classname = "MyClass"; var_dump($classname::e()); // also works! int(5)
Note that after the object operator, the $ should not be written ($object->a instead of $object->$a). For the class operator, this is not the case and the $ is necessary. For a constant defined in the class, the $ is never used.
Also note that var_dump(MyClass::d()); is only allowed if the function d() does not reference the object:
class MyClass { private $a = 1; public function d() { return $this->a; } } $object = new MyClass(); var_dump(MyClass::d()); // Error!
This causes a 'PHP Fatal error: Uncaught Error: Using $this when not in object context'
These operators have left associativity, which can be used for 'chaining':
class MyClass { private $a = 1; public function add(int $a) { $this->a += $a; return $this; } public function get() { return $this->a; } } $object = new MyClass(); var_dump($object->add(4)->get()); // int(5)
These operators have the highest precedence (they are not even mentioned in the manual), even higher that clone. Thus:
class MyClass { private $a = 0; public function add(int $a) { $this->a += $a; return $this; } public function get() { return $this->a; } } $o1 = new MyClass(); $o2 = clone $o1->add(2); var_dump($o1->get()); // int(2) var_dump($o2->get()); // int(2)
The value of $o1 is added to before the object is cloned!
Note that using parentheses to influence precedence did not work in PHP version 5 and older (it does in PHP 7):
// using the class MyClass from the previous code $o1 = new MyClass(); $o2 = (clone $o1)->add(2); // Error in PHP 5 and before, fine in PHP 7 var_dump($o1->get()); // int(0) in PHP 7 var_dump($o2->get()); // int(2) in PHP 7
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework