PHP finding the expired time in seconds - php

Let say we have start date and end date
if (isset($_POST['start_date']))
$_POST['start_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['start_date']));
if (isset($_POST['end_date']))
$_POST['end_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['end_date']));
would $_POST['end_date'] - $_POST['start_date'] give you the expired time in seconds?

No, to get the expired time in seconds you would use strtotime():
$expired = strtotime($_POST['end_date'])-strtotime($_POST['start_date']);
if (isset($_POST['start_date']))
$_POST['start_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['start_date']));
if (isset($_POST['end_date']))
$_POST['end_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['end_date']));
if (isset($_POST['start_date']) && isset($_POST['end_date'])) echo 'Expired time in seconds: ' . $expired;

I suggest you to use mktime function to get the unix timestamp from a date literal, which is in milliseconds.
$startDate = mktime($hour, $minute, $second, $month, $day, $year);
$endDate = mktime($hour, $minute, $second, $month, $day, $year);
$diffInSeconds = ($endDate - $startDate) / 1000; // 1 second = 1000 miliseconds

Related

get same time next year php

I get this out
time: 11:15:04 date: Oct 26, 2015
when i click the function calling this code
$date = date("Y");
$timestamp = strtotime($date);
$isLeapYear = date("L", $timestamp);
($isLeapYear == 1)? $time_interval = 366 : $time_interval = 365;
$time = time() + $time_interval;
$hours = strftime("%H:%M:%S ",$time);
$date = strftime("%b %d, %Y",$time);
echo 'time: '. $hours;
echo 'date: '. $date;
My time in computer is
My question is how can i set the time correctly?
Where did the time come from?
How can i get the correct time for next year?
What about using the DateTime class? It can be really helpful for this kind of operations:
$date = new DateTime();
$date->add(new DateInterval("P1Y"));
If you just want the exact same time and date plus one year:
you could do:
$time = strtotime("+1 year");
and display:
$hours = strftime("%H:%M:%S ",$time);
$date = strftime("%b %d, %Y",$time);
echo 'time: '. $hours;
echo 'date: '. $date;
for this code to work, you have to set the correct time zone and time set on your server.
instead of writing the whole time and date format, you can set the correct locale.
as example
setlocale(LC_ALL, 'de_CH', 'de_CH.ISO8859-1', 'de_CH.ISO8859-15', 'de_CH.UTF-8');
echo strftime("%x %X ",$time);
//will output
26.10.2016 11:27:23
Set timezone use date_default_timezone_set():-
//add this line
date_default_timezone_set("Asia/Bangkok");//your timezone
$date = date("Y");
$timestamp = strtotime($date);
$isLeapYear = date("L", $timestamp);
($isLeapYear == 1)? $time_interval = 366 : $time_interval = 365;
$time = time() + $time_interval;
$hours = strftime("%H:%M:%S ",$time);
$date = strftime("%b %d, %Y",$time);
echo 'time: '. $hours;
echo 'date: '. $date;
The date_default_timezone_set() function sets the default timezone
used by all date/time functions.

PHP time function displays wrong value

I am using following code
list($date, $time) = explode(' ', $row['created_at']);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timemodified = mktime($hour, $minute, $second, $month, $day, $year);
$threshold = time() - 6;
echo $threshold.'</br>';
echo $timemodified.'</br>';
echo $timemodified - $threshold;
It outputs
1428631618
1428643990
12372
The modified time is just two minutes ago. Why is the difference so big I am just subtracting six seconds. Am I missing sommething?
It's because the $threshold time is not really 6 seconds. You can use strtotime() function to subtract 6 seconds from your time
$newTime = strtotime('-6 seconds', $timemodified);
echo date('Y-m-d H:i:s', $newTime);
hope this helps. For my example see this: http://codepad.org/cRp858RG

convert between timestamp and date time

I tried this code,it will give me the right date but the time is not correct:
function convert_datetime($str) {
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute) = explode(':', $time);
$timestamp = mktime($hour, $minute, $year, $month, $day);
return $timestamp;
}
if(isset($_POST['submit']))
{
$datetime=$_POST['startdate'].' '.$_POST['start_hour'].":".$_POST['start_minute'];
$timestamp=convert_datetime($datetime);
echo "DateTime:".$datetime;
echo " ";
echo "Timestamp:".$timestamp;
echo " ";
$dateandtime = date("Y-m-d H:i", $timestamp);
echo "converted:".$dateandtime;
}
with input: 2013-1-21 21:51
I will get this out put
DateTime:2013-1-21 21:51 Timestamp:1358807073 converted:2013-01-21 22:24
so the order is not correct.and in the time part I have problem.How to fix this?
Use Datetime. It's much easier and more accurate:
$datetime = DateTime::createFromFormat("Y-m-d H:i", '2013-1-21 21:51');
echo 'Timestamp: ' . $datetime->getTimestamp() . PHP_EOL;
echo 'Datetime: ' . $datetime->format("Y-m-d H:i") . PHP_EOL;
Confirmed working
You are missing the seconds argument - see mktime on phpdocs. In your example seconds is being supplied the value 2013, which when added to the time alters the overall result.
function convert_datetime($str) {
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute) = explode(':', $time);
$timestamp = mktime($hour, $minute, 0, $year, $month, $day);
return $timestamp;
}
On a side note, php does have conversion functions built in. Try strtotime.
Put in a zero field for seconds when you are passing the time. I believe it is taking 2013 seconds and using it to add 2013/60 and using it to add 33 minutes to your time. I believe that mktime assumes current date for missing fields, which is why it is still getting 2013 for the year.

counting hours and adding minutes in time in php

everyone i have a problem i can't count the hours in time....
this is my codes:
$seconds =mktime (0,0,0);
$hours = (int)($seconds / (60 * 60));
can anyone help me determine what's my wrong?..
THANKS!
Use PHP's strtotime() function which manipulates timestamp.
Examples:
<?php
$one_minute_later = date('Y-m-d H:i:s', strtotime('+1 minute'));
// Same as
$one_minute_later = date('Y-m-d H:i:s', time() + 60);
$one_hour_later = date('Y-m-d H:i:s', strtotime('+1 day'));
$one_day_later = date('Y-m-d H:i:s', strtotime('+1 day'));
$now = time(); // Current timestamp

Add 30 seconds to the time with PHP

How can I add 30 seconds to this time?
$time = date("m/d/Y h:i:s a", time());
I wasn't sure how to do it because it is showing lots of different units of time, when I only want to add 30 seconds.
$time = date("m/d/Y h:i:s a", time() + 30);
If you're using php 5.3+, check out the DateTime::add operations or modify, really much easier than this.
For example:
$startTime = new DateTime("09:00:00");
$endTime = new DateTime("19:00:00");
while($startTime < $endTime) {
$startTime->modify('+30 minutes'); // can be seconds, hours.. etc
echo $startTime->format('H:i:s')."<br>";
break;
}
What about using strtotime? The code would then be:
strtotime( '+30 second' );
$time = date("m/d/Y h:i:s a", time() + 30);
//or
$time = date("m/d/Y h:i:s a", strtotime("+30 seconds"));
General :
$add_time=strtotime($old_date)+30;
$add_date= date('m/d/Y h:i:s a',$add_time);
See mktime:
mktime (date("H"), date("i"), date("s") + 30)
http://www.php.net/manual/en/function.mktime.php
should do what you want.
$time = date("m/d/Y h:i:s", time());
$ts = strtotime($time);
$addtime = date("m/d/Y h:i:s", mktime(date("h", $ts),date("i", $ts),date("s", $ts)+30,date("Y", $ts),date("m", $ts),date("d", $ts));
Would be a more explained version of all of the above.

Categories