0% found this document useful (0 votes)
1 views

Lab Programs- 16-20

The document contains multiple PHP lab programs demonstrating various functionalities including date and time operations, file uploads, database creation, table creation, and a college admission form. Each section provides code snippets for implementing these features, along with explanations of their purpose. The programs utilize MySQL for database interactions and include error handling for better user experience.

Uploaded by

suraiyareeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab Programs- 16-20

The document contains multiple PHP lab programs demonstrating various functionalities including date and time operations, file uploads, database creation, table creation, and a college admission form. Each section provides code snippets for implementing these features, along with explanations of their purpose. The programs utilize MySQL for database interactions and include error handling for better user experience.

Uploaded by

suraiyareeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Program 16

Write a PHP script to Show the Functionality of Date and Time


Functions.
<?php

echo "<h2> PHP script to Show the Functionality of Date and Time Functions:
</h2>";

// Set the default timezone to avoid warnings


date_default_timezone_set("Asia/Kolkata");

// 1. date() - Format a local date and time


echo "<b>1. date() - Format a local date and time:</b><br>";
echo "Current date and time: " . date("Y-m-d H:i:s") . "<br><br>";

// 2. strtotime() - Parse an English textual datetime into a Unix timestamp


echo "<b>2. strtotime() - Parse an English textual datetime into a Unix
timestamp:</b><br>";
$date = "next Friday";
$timestamp = strtotime($date);
echo "Timestamp for '$date': " . $timestamp . "<br>";
echo "Formatted date for this timestamp: " . date("Y-m-d H:i:s", $timestamp) .
"<br><br>";

// 3. date_create() - Returns a new DateTime object


echo "<b>3. date_create() - Returns a new DateTime object:</b><br>";
$dateTime = date_create("2025-12-31 15:30:00");
echo "Created DateTime: " . date_format($dateTime, "Y-m-d H:i:s") .
"<br><br>";

// 4. date_add() - Adds days, months, years, hours, minutes, and seconds to a


date
echo "<b>4. date_add() - Add time to a DateTime object:</b><br>";
date_add($dateTime, date_interval_create_from_date_string("10 days"));
echo "Date after adding 10 days: " . date_format($dateTime, "Y-m-d H:i:s") .
"<br><br>";

// 5. date_diff() - Returns the difference between two dates


echo "<b>5. date_diff() - Difference between two dates:</b><br>";
$date1 = date_create("2023-01-01");
$date2 = date_create("2025-01-01");
$diff = date_diff($date1, $date2);
echo "Difference between 2023-01-01 and 2025-01-01: " . $diff->format("%y
years, %m months, %d days") . "<br><br>";

// 6. mktime() - Returns the Unix timestamp for a date


echo "<b>6. mktime() - Returns the Unix timestamp for a date:</b><br>";
$timestamp = mktime(15, 30, 0, 12, 31, 2025);
echo "Unix timestamp for 2025-12-31 15:30:00: " . $timestamp . "<br>";
echo "Formatted date: " . date("Y-m-d H:i:s", $timestamp) . "<br><br>";

// 7. time() - Returns the current time as a Unix timestamp


echo "<b>7. time() - Current Unix timestamp:</b><br>";
echo "Current Unix timestamp: " . time() . "<br>";

?>
Lab Program 17
Write a PHP Program to Upload a File.
<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
<title>PHP File Upload</title>
</head>
<body>

<h2>PHP Program to Upload a File: </h2>

<?php
if (isset($_SESSION['message'])) {
echo '<p><b>' . $_SESSION['message'] . '</b></p>';
if (isset($_SESSION['uploadedFilePath'])) {
echo '<p>Click to view the file: <a href="' .
$_SESSION['uploadedFilePath'] . '" target="_blank">View Uploaded
File</a></p>';
}
unset($_SESSION['message']);
unset($_SESSION['uploadedFilePath']);
}
?>

<form method="POST" action="fileUpload.php" enctype="multipart/form-data">


<label>Select a file to upload:</label><br>
<input type="file" name="uploadedFile" required /><br><br>
<input type="submit" name="uploadBtn" value="Upload File" />
</form>

</body>
</html>
<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['uploadedFile'])) {


$file = $_FILES['uploadedFile'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowed = ['jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc', 'pdf'];

if ($file['error'] === UPLOAD_ERR_OK && in_array($ext, $allowed)) {


$uploadDir = './uploads/';
$newName = md5(time() . $file['name']) . '.' . $ext;
$path = $uploadDir . $newName;

if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);

if (move_uploaded_file($file['tmp_name'], $path)) {
$_SESSION['message'] = '✅ File uploaded successfully.';
$_SESSION['uploadedFilePath'] = $path;
} else {
$_SESSION['message'] = '❌ Failed to move uploaded file.';
}
} else {
$_SESSION['message'] = '❌ Upload failed or invalid file type.';
}
} else {
$_SESSION['message'] = '❌ No file uploaded.';
}

header('Location: LP17.php');
exit();
Lab Program 18
Write a PHP Script to Implement Database Creation.
<?php

echo "<h2>PHP Script to Implement Database Creation: </h2>";

$servername = "localhost";
$username = "root";
$password = ""; // Leave this empty for XAMPP default
$dbname = "new_database";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("❌ Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE $dbname";
if ($conn->query($sql) === TRUE) {
echo "✅ Database '$dbname' created successfully";
} else {
echo "❌ Error creating database: " . $conn->error;
}

// Close connection
$conn->close();
?>
http://localhost/phpmyadmin
Lab Program 19
Write a PHP Script to Create a Table.
<?php

echo "<h2>PHP Script to Create a Table: </h2>";

// Database credentials
$servername = "localhost"; // Server name
$username = "root"; // MySQL username (default for XAMPP is 'root')
$password = ""; // MySQL password (default for XAMPP is empty)
$dbname = "new_database"; // The database where the table will be created

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("❌ Connection failed: " . $conn->connect_error);
}

// SQL to create a table


$sql = "CREATE TABLE MyGuests (
id INT(6) PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";

// Check if table was created successfully


if ($conn->query($sql) === TRUE) {
echo "✅ Table 'MyGuests' created successfully!";
} else {
echo "❌ Error creating table: " . $conn->error;
}

// Close the connection


$conn->close();
?>
Lab Program 20
Develop a PHP Program to Design a College Admission Form Using
MySQL Database.
<?php

echo "<h2> PHP Program to Design a College Admission Form Using MySQL
Database: </h2>";

// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "college_admission";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("❌ Connection failed: " . $conn->connect_error);
}

// Create database if it doesn't exist


$sql_create_db = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($sql_create_db) === TRUE) {
echo "✅ Database '$dbname' exists or was created successfully!<br>";
} else {
echo "❌ Error creating database: " . $conn->error . "<br>";
}

// Use the created database


$conn->select_db($dbname);

// Create table if it doesn't exist


$sql_create_table = "CREATE TABLE IF NOT EXISTS admission_form (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
phone_number VARCHAR(15),
course VARCHAR(50),
gender ENUM('Male', 'Female'),
date_of_birth DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
if ($conn->query($sql_create_table) === TRUE) {
echo "✅ Table 'admission_form' exists or was created successfully!<br>";
} else {
echo "❌ Error creating table: " . $conn->error . "<br>";
}

// Handle form submission


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Collect form data
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone_number = $_POST['phone_number'];
$course = $_POST['course'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];

// Prepare the SQL query to insert form data into the database
$sql_insert = "INSERT INTO admission_form (first_name, last_name, email,
phone_number, course, gender, date_of_birth)
VALUES ('$first_name', '$last_name', '$email',
'$phone_number', '$course', '$gender', '$dob')";

if ($conn->query($sql_insert) === TRUE) {


echo "Admission details have been successfully submitted!<br>";
} else {
echo "Error: " . $sql_insert . "<br>" . $conn->error . "<br>";
}
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>College Admission Form</title>
</head>
<body>
<h2>College Admission Form</h2>
<form action="" method="POST">
<label for="first_name">First Name:</label><br>
<input type="text" id="first_name" name="first_name" required><br><br>

<label for="last_name">Last Name:</label><br>


<input type="text" id="last_name" name="last_name" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>

<label for="phone_number">Phone Number:</label><br>


<input type="text" id="phone_number" name="phone_number"><br><br>

<label for="course">Course:</label><br>
<input type="text" id="course" name="course"><br><br>

<label for="gender">Gender:</label><br>
<select id="gender" name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br><br>

<label for="dob">Date of Birth:</label><br>


<input type="date" id="dob" name="dob"><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

<?php
// Close connection
$conn->close();
?>

You might also like