Ahmad Saad
Ahmad Saad

Reputation: 803

How to calculate time in php

help me in calculating exact time with AM and PM

$time1 = strtotime(10:00); /// in pm 
$time1 = strtotime(08:00); ///// answer should come in am calculation
                                 is exact but not the am pm value
$count = date ('g:i ' ,strtotime($time1 . '+' . $time2));

My Problem is if i calculate time i calculate it correctly as i want but how can i set the PM and AM to time if i take 10:00 PM time add 4 hour to it now time is 02:00 AM how to get that AM PM value with time and any help with subtract time how we subtract time

Upvotes: 0

Views: 224

Answers (2)

Oscar M.
Oscar M.

Reputation: 1086

Use DateTime to deal with Dates and Times with less hassle.

<?php
$time = new DateTime("10am"); // assumes 10am today
$time->modify("+4 hours");
echo $time->format("g:i A"); // outputs 2:00 PM

See the DateTime book by the author of the DateTime extension.

Upvotes: 1

Marc B
Marc B

Reputation: 360912

All you need is:

$t1 = strtotime('10am'); // 1399910400 on the date this was written
$t2 = strtotime('8am'); // 1399903200 on the date this was written

But note that that you CANNOT just do $t1 + $t2. You'll actually be doing

$t3 = 1399910400 + 1399903200
$t3 = 2799813600

And the timestamp 2799813600 corresponds to Sep 21, 2058.

You need to do:

'8am' + 2 hours => '10am';

which is

$t3 = strtotime('8am') + 60 * 60 * 2;
                         ^^^^^^^^^^^---2hours

Upvotes: 0

Related Questions