PHP find output programs (Date and Time) | set 1

Find the output of PHP programs | Date and Time | Set 1: Enhance the knowledge of PHP Date and Time concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 26, 2021

Question 1:

<?php
    $today = date("Y#m#d");
    
    printf("Today's Date  %s", $today);
    echo '<br>';
    
    $today++;
    printf("Tomorrow's Date  %s", $today);
?>

Output:

Today's Date 2021#01#26
Tomorrow's Date 2021#01#27

Explanation:

In the above program, we created a variable $today, which is initialized with the value returned by date() function.

The date() function will return current date. Here, we used characters to represent year, month, and date.

Here 'Y' will represent the current year 2020, 'm' represent month number '06', and' represent the date that is '21', And, we used symbol '#' to separate date, month, and year.

printf("Today's Date  %s",$today);

The printf() will print today's date, and then we used increment operator to increase date, then it will print tomorrow's date.

Question 2:

<?php
    $cur_time = date("h:i:s");
    printf("%s", $cur_time);
    echo '<br>';
    
    $cur_time++;
    printf("%s", $cur_time);
?>

Output:

03:19:53
03:19:54

Explanation:

In the above program, we created a variable $cur_time, which is initialized with the value returned by date() function.

The date() function will return current time. Here, we used characters to represent the hour, minute, and second.

Here 'h' will represent current year 03, 'i' represent minutes '19', and 's' will represent seconds that is '53', And we used symbol ':' to separate hour, minute, and second.

printf("%s",$cur_time);

The printf() will print the current time, and then we used increment operator to increase one second, then it will print increased time.

Question 3:

<?php
    $var = date("a");
    
    $var++;
    
    echo $var;
?>

Output:

an

Explanation:

The above program will print "an" on the webpage, the below statement will return meridiem name in lowercase,

$var = date("a");
  • "AM" - Ante meridiem
  • "PM" - Post meridiem

Then we used increment operator with $var, then it will print "an", and it will print "pn" for "pm".

Question 4:

<?php
    date_default_timezone_set("Asia/Kolkata");
    
    $time = date("h:i:sa");
    
    echo $time;
?>

Output:

09:46:44am

Explanation:

In the above program, we set time-zone "Asia/Kolkata", And the date() function will return current time, and assigned to variable $time. Then we will print variable $time that will print "09:46:44am" on the webpage.

Question 5:

<?php
    $newdate = mktime(10, 14, 54, 6, 21, 2020);
    echo "Date : " . date("Y-m-d h:i:sa", $newdate);
?>

Output:

Date : 2020-06-21 10:14:54am

Explanation:

In the above program, we used the mktime() function, which is used to create a date with specified values.

The syntax of mktime() given below:

mktime(hour, minute, second, month, day, year);

Here, newly created date assigned to $newdate then we printed the value of $newdate on the webpage.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.