2018-11-20 22:10
浏览 124

将日期输入转换为12小时格式PHP [重复]

This question already has an answer here:

I need some help figuring out how to change a date input field into a 12 hour format so I can pass the variable to a email sending script. I am trying to be able to have a online time sheet so that employees can enter there time online. Problem is I need the time, 12 hour format so that the accountant can better read the times and enter them faster. I am VERY new to PHP and would appreciate any help. Comment with any questions.

This is what it look like when it emails:



<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">


<div class="content">
    <form class="timesheetform" action="form.php" method="post">
      <p class="header">GRADY ELECTRIC<br>TIME SHEET</p>
      <input type="text" name="employee" placeholder="Employee Name">
      <input type="text" name="email" placeholder="Email">
      <input type="number" name="job#" placeholder="Job # (see work order log)">
      <p class="mini-heading">Date:</p>
      <input class="across" type="date" name="date">

      <p class="sub-heading">Start Time:</p>
      <input class="across" type="time" name="starttime" placeholder="Start Time">

      <p class="sub-heading">Lunch Duration:</p>
      <input type="number" name="lunch" placeholder="(Enter In Minutes)">

      <p class="sub-heading across">Stop Time:</p>
      <input class="across" type="time" name="stoptime" placeholder="Stop Time">

      <p class="sub-heading">Total Hours:</p>
      <input type="text" name="totalhours" placeholder="Total Hours">

      <p class="sub-heading">Drive Time:</p>
      <input type="text" name="drivetime" placeholder="Drive Time">

      <p class="sub-heading">Job Complete?</p>

<div class="radiobuttons">
      <div class="radiobtnoption">
        <input type="radio" name="radiobtn" value="YES">
      <div class="radiobtnoption">
        <input type="radio" name="radiobtn" value="NO">

      <input type="text" name="customer" placeholder="Customer (ex: Brascia)">
      <input type="text" name="location" placeholder="Location/Address">
      <textarea name="jobdescription" placeholder="Job Description"></textarea>

  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-1" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-1" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-1" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-1" placeholder="Total">
  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-2" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-2" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-2" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-2" placeholder="Total">
  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-3" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-3" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-3" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-3" placeholder="Total">
  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-4" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-4" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-4" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-4" placeholder="Total">
  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-5" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-5" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-5" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-5" placeholder="Total">
  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-6" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-6" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-6" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-6" placeholder="Total">
  <div class="materialssection">
    <input class="materialsinfo" type="text" name="m-7" placeholder="Materials:">
    <input class="materialsinfo" type="text" name="q-7" placeholder="Qty:">
    <input class="materialsinfo" type="text" name="e-7" placeholder="Each:">
    <input class="materialsinfo" type="text" name="t-7" placeholder="Total">

      <textarea name="notes" placeholder="Notes:"></textarea>
      <button type="submit" name="submit">Submit</button>



if (isset($_POST['submit'])) {

$email = $_POST['email'];
$to = ", $email";
$subject = "New TimeSheet";
$name = $_POST['employee'];
$date = $_POST['date'];
$jobnum = $_POST['job#'];
$radio = $_POST['radiobtn'];
$customer = $_POST['customer'];
$address = $_POST['location'];
$jobdes = $_POST['jobdescription'];
$starttime = mktime($_POST['starttime']);
$lunchtime = $_POST['lunch'];
$stoptime = mktime($_POST['stoptime']);
$totalhours = $_POST['totalhours'];
$drivetime = $_POST['drivetime'];
$notes = $_POST['notes'];

$m1 = $_POST['m-1'];
$m2 = $_POST['m-2'];
$m3 = $_POST['m-3'];
$m4 = $_POST['m-4'];
$m5 = $_POST['m-5'];
$m6 = $_POST['m-6'];
$m7 = $_POST['m-7'];

$q1 = $_POST['q-1'];
$q2 = $_POST['q-2'];
$q3 = $_POST['q-3'];
$q4 = $_POST['q-4'];
$q5 = $_POST['q-5'];
$q6 = $_POST['q-6'];
$q7 = $_POST['q-7'];

$e1 = $_POST['e-1'];
$e2 = $_POST['e-2'];
$e3 = $_POST['e-3'];
$e4 = $_POST['e-4'];
$e5 = $_POST['e-5'];
$e6 = $_POST['e-6'];
$e7 = $_POST['e-7'];

$t1 = $_POST['t-1'];
$t2 = $_POST['t-2'];
$t3 = $_POST['t-3'];
$t4 = $_POST['t-4'];
$t5 = $_POST['t-5'];
$t6 = $_POST['t-6'];
$t7 = $_POST['t-7'];

$message = "<html><body>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: lightgrey;'><td><strong>Tech Name:</strong> </td><td>" .$name. "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Job #:</strong> </td><td>" .$jobnum. "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Date:</strong> </td><td>" . $date . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Start Time:</strong> </td><td>" . date("h:i:a", $starttime) . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Lunch Duration:</strong> </td><td>" . $lunchtime . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Stop Time:</strong> </td><td>" . date("h:i:a", $starttime) . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Total Hours:</strong> </td><td>" . $totalhours . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Drive Time:</strong> </td><td>" . $drivetime . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Job Complete:</strong> </td><td>" . $radio . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Customer:</strong> </td><td>" . $customer . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Location/Address:</strong> </td><td>" . $address . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Job Description:</strong> </td><td>" . $jobdes . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Notes:</strong> </td><td>" . $notes . "</td></tr>";
$message .= "<tr style='background: lightgrey;'><td><strong>Materials:</strong></td> <td><strong>Qty:</strong></td> <td><strong>Each:</strong></td> <td><strong>Total:</strong></td></tr>";
$message .= "<tr style='background: lightgrey;'><td>$m1</td><td>$q1</td><td>$e1</td><td>$t1</td>";
$message .= "<tr style='background: lightgrey;'><td>$m2</td><td>$q2</td><td>$e2</td><td>$t2</td>";
$message .= "<tr style='background: lightgrey;'><td>$m3</td><td>$q3</td><td>$e3</td><td>$t3</td>";
$message .= "<tr style='background: lightgrey;'><td>$m4</td><td>$q4</td><td>$e4</td><td>$t4</td>";
$message .= "<tr style='background: lightgrey;'><td>$m5</td><td>$q5</td><td>$e5</td><td>$t5</td>";
$message .= "<tr style='background: lightgrey;'><td>$m6</td><td>$q6</td><td>$e6</td><td>$t6</td>";
$message .= "<tr style='background: lightgrey;'><td>$m7</td><td>$q7</td><td>$e7</td><td>$t7</td>";
$message .= "</table>";
$message .= "</body></html>";

// Always set content-type when sending HTML email
$headers = "From:
$headers .= "Content-Type: text/html";

mail( $to, $subject, $message, $headers );
header("location:",  true,  301 );  exit;
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutouhe5343 2018-11-20 22:30

    Take your time input values, e.g. 17:00:00, and convert it to a Unix timestamp using the strtotime() function.

    $t = strtotime('17:00:00');

    You can then use the date() function to format the timestamp value, e.g. 05:00 am.

    echo date('h:i a', $t);  // 05:00 am

    Two points for your consideration:

    1. Not all browsers support the time input field, e.g. Safari.
    2. In your HTML output, you're using the $startTime value for the stop time.
    打赏 评论

相关推荐 更多相似问题