PHP time function displays wrong value - php

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

Related

PHP Comparing two dates but Hours not functioning correctly

I'm having an issue with my code not displaying the correct difference between dates. The Days Minutes and Seconds all work correctly but the Hours seem to be displayed the subtracted amount and not the remainder if that makes sense at all.
For example, using these dates
2171167 = 2013-05-18 00:00:00 - 2013-04-22 20:53:53
I receive the following output
25 days 19:06:07
$date_one = date('Y-m-d H:i:s');
$date_two = date('Y-m-d H:i:s', mktime(0, 0, 0, 5, 18, 2013));
$Difference = abs(strtotime($date_two) - strtotime($date_one));
$Days = date("d", $Difference);
//$Hours = date("H", $Difference); Why does this NOT WORK???
$Minutes = date("i", $Difference);
$Seconds = date("s", $Difference);
If you could please tell me why the second "Hours" variable i have commented out is not working i would very much appreciate it.
<?php
header('Content-Type: text/plain');
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2013-05-18 00:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2013-04-22 20:53:53');
$result = $date1->diff($date2);
echo $result->format('%Y-%m-%d %H:%i:%s');
?>
Shows:
00-0-25 03:6:7
To split into variables:
list($year, $month, $day, $hour, $minute, $second) = explode('-', $result->format('%Y-%m-%d-%H-%i-%s'));
var_dump($year, $month, $day, $hour, $minute, $second);
Shows:
string(2) "00"
string(1) "0"
string(2) "25"
string(2) "03"
string(1) "6"
string(1) "7"
you are using wrong first date use below code to your actual answer
$date_one = "2013-04-22 20:53:53"; //date('Y-m-d H:i:s');
$date_two = date('Y-m-d H:i:s', mktime(0, 0, 0, 5, 18, 2013));
$Difference = abs(strtotime($date_two) - strtotime($date_one));
echo "<br> dif ->".date('d H:i:s',$Difference);
echo "<br> day -> ".$Days = date("d", $Difference);
echo "<br> Hours -> ".$Hours = date("H", $Difference);
echo "<br> Minutes -> ".$Minutes = date("i", $Difference);
echo "<br> Seconds -> ".$Seconds = date("s", $Difference);
:OUTPUT:
dif -> 26 03:06:07
day -> 26
Hours -> 03
Minutes -> 06
Seconds -> 07
Just change the hours syntax.
<?php
$date_one = date('Y-m-d H:i:s');
$date_two = date('Y-m-d H:i:s', mktime(0, 0, 0, 5, 18, 2013));
$Difference = round(strtotime($date_two) - strtotime($date_one));
$Days = date("d", $Difference);
$Hours = date("H", $Difference);
echo $Hours = $Difference / 60;
$Minutes = date("i", $Difference);
$Seconds = date("s", $Difference);
?>

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.

PHP finding the expired time in seconds

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

Count down to EST Hour everyday?

How would i count down to a specific hour every day, say 12PM EST time... So that it would countdown as 1 Hour 56 Minutes until 12PM / Lunch? I'm looking for something short and simple, no need for CSS or JS as it will be text and only update when the page is refreshed.
I'm not sure if you have heard, but there's this trillion dollar site called google.com. You can search terms that will give you the best results on the web! Try it sometime, hella useful.
Check it out, here's the first result from it!
http://www.tripwiremagazine.com/2011/04/9-cool-jquery-countdown-scripts.html
Here's what i was looking for in case it will help someone else.
function countdown($year, $month, $day, $hour, $minute)
{
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
$current = time();
$difference = $the_countdown_date - $current;
if ($difference < 0) $difference = 0;
$days = floor($difference/60/60/24);
$hours = floor(($difference - $days*60*60*24)/60/60);
$minutes = floor(($difference - $days*60*60*24 - $hours*60*60)/60);
echo "Checkback in ".$hours." hours and ".$minutes." minutes";
}
countdown(2016,1,1,12,0);

PHP - Replace split [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 11 months ago.
I have these 2 functions in which I have to replace split with another php command:
function date_fr_mysql($date) {
list($jour,$mois,$annee)=split("/",$date);
$date = $annee."-".$mois."-".$jour;
return $date;
}
function date_mysql_fr($date) {
list($annee,$mois,$jour)=split("-",$date);
$date = $jour."/".$mois."/".$annee;
return $date;
}
with which function I can replace it to get the same result?
You can use the explode function.
The function explode is similar to split, except it does not regexes. Use preg_split if you need regex support.
I have this example from PHP manual on split function over date:
<?php
// Delimiters may be slash, dot, or hyphen `
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year";
?>
After some experimentation, the solution to avoid a deprecated warning and still have the same result is:
<?php
// Delimiters may be slash, dot, or hyphen
// test preg_split per /
$valore2 = "2010/01/01";
//list($month, $day, $year) = split('[/.-]', $valore2);
list($year, $month, $day) = preg_split('[/|\.|-]', $valore2);
echo "Month: $month; Day: $day; Year: $year\n";
// test preg_split per -
$valore2 = "2010-01-01";
//list($month, $day, $year) = split('[/.-]', $valore2);
list($year, $month, $day) = preg_split('[/|\.|-]', $valore2);
echo "Month: $month; Day: $day; Year: $year\n";
// test preg_split per .
$valore2 = "2010.01.01";
//list($month, $day, $year) = split('[/.-]', $valore2);
list($year, $month, $day) = preg_split('[/|\.|-]', $valore2);
echo "Month: $month; Day: $day; Year: $year\n";
?>
Hope this helps.
Given it seems you're just changing - to /, how about
$date = str_replace('-', '/', $date);
date ( 'Y-m-d', strtotime ( $your_date ) );

Categories