If I have a random unix timestamp, how can I round it down to today's midnight or the midnight selected by the user. The reason for this is that I want to add hours and minutes after a certain day's midnight.
For example if the timestamp is 1324189035 then how can I remove the hours, minutes, and seconds to put the timestamp at midnight for that day.
echo date('d-m-Y H:i:s', strtotime('today', 1324189035));
Because of how you're using it, I wouldn't calculate midnight at all: it is far easier to simply convert what you're adding to the timestamp into 24 hour time and then use strtotime:
echo strtotime("0:00",1324189035); // 1324184400
echo strtotime("17:50",1324189035); // 1324248600
And if you want to have that in human readable, use date and m/d/Y H:i:s:
echo date('m/d/Y H:i:s', strtotime('17:50',1324189035)); // 12/18/2011 17:50:00
Simply Use
strtotime('today midnight');
Just do
date('d-m-Y',strtotime('today'));
Easy!
An easy solution would be to use the modulo expression to remove the exceeded seconds from a round day timestamp.
<?php
date_default_timezone_set('UTC');
$timestamp = time();
echo "timestamp : " . $timestamp . PHP_EOL;
echo "timestamp formatted : " . date('Y-m-d H:i:s', $timestamp) . PHP_EOL;
$diff = $timestamp % (60 * 60 * 24);
echo "diff : " . $diff . PHP_EOL;
$midnight = $timestamp - $diff;
echo "midnight : " . $midnight . PHP_EOL;
echo "midnight formatted : " . date('Y-m-d H:i:s', $midnight) . PHP_EOL;
This would output the following result.
timestamp : 1575451074
timestamp formatted : 2019-12-04 09:17:54
diff midnight : 1575417600
midnight formatted : 2019-12-04 00:00:00
And here is a one liner function to get your midnight from any timestamp.
function getMidnight ($timestamp) { return $timestamp - ($timestamp % 86400); }
http://sandbox.onlinephpfunctions.com/code/1d85c935e71fcf011284ae33658e0c68dd8d8c28
How about just:
date -d $(date +%F) +%s
Related
im doing some stuff with mktime, i need to add the next date with 30 days more but its returning me 1970-01-30 date, what im doing wrong ?
$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime);
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$fecha),date("j",$fecha)+30,date("Y",$fecha));
echo date("Y-m-d",$nueva_fecha)."<br />";
Result:
2013-10-04
1970-01-30
Date is looking for a timestamp as it's 2nd parameter, not a string value representing this. Updated to pass it $strtime instead.
$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime); // <-- Unnecessary unless you want to echo the value.
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$strtime),date("j",$strtime)+30,date("Y",$strtime));
echo date("Y-m-d",$nueva_fecha)."<br />";
Output:
2013-10-04
2013-11-03
You can just use the following function to add 30 days to the date you put in:
$date = strtotime("2013-10-04");
$new_date = strtotime("+30 days", $date);
or simply to the current date:
$new_date = strototime("+30 days", time());
If you already have strtotime, why plus on date ? Instead you could've used + (30 days in seconds) OR simply the feature they offer you + 1 day check answer: adding one day to a date
strtotime('2013-10-04 + 30 days');
This will print 2013-11-03:
date('Y-m-d', strtotime('2013-10-04 + 30 days'))
you can try this:
echo strtotime("+1 day"), "\n";
echo strtotime("+30 day",strtotime(date('D, d M Y'))), "\n";
this will add 30 days to the current date.
Also strtotime is very usefull you can use it for weekly,monthly and yearly.
You can use this also
<?php
$date = date("Y/m/d"); // example date in yyyy/mm/dd format
$unix_time = strtotime( $date ); // covert date to unix time
$sec_in_30_days = 60 * 60 * 24 * 30; // 60 seconds * 60 minutes * 24 hours * 30 days
$new_unix_time = $unix_time + $sec_in_30_days; // add 30 days to unix time
$date_in_30_days = date( 'Y/m/d', $new_unix_time ); // convert new unix time to date
// Output results:
echo 'original current date: ' . $date . '<br />';
echo '<br />';
echo 'new date: ' . $date_in_30_days . '<br />';
?>
Output will be
original current date: 2013/10/04
new date: 2013/11/03
I am doing the following in my php script and I am not seeing the results I though I would:
$current_date_num = strtotime("now");
echo $current_date_num ." BEFORE DATE HOUR<br/>";
$new_current = date('Y-m-d h:i:s', strtotime($current_date_num));
echo $new_current . " AFTER DATE<br/>";
$current_date_num = date('Y-m-d h:i:s', strtotime($current_date_num) - 60 * 60 * 6);
echo $current_date_num ." CURRENT<br/>";
$end_date = strtotime("+1 day");
$end_date = date("Y-m-d h:i:s",$end_date);
echo $end_date ." END DATE";
This is my output and Only the END DATE is showing what I would expect.
1322673564 BEFORE DATE HOUR
1969-12-31 07:00:00 AFTER DATE
1969-12-31 01:00:00 CURRENT
2011-12-01 12:19:24 END DATE
What I thought would happen is I get the Unix timestamp and then use strtotime and get the current time and then the current time minus 6 hours and finally the time 24 hours from now. Not sure how I am messing this up so bad?
You are applying strtotime() two times too many. One time in line 4:
strtotime($current_date_num)
remove that, and the strtotime() call in line 7.
strtotime takes a human-readable time and parses it into a timestamp. As you already have a timestamp, the strptime calls are generating bogus data.
$current_date_num = strtotime("now");
echo $current_date_num ." BEFORE DATE HOUR<br/>";
$new_current = date('Y-m-d h:i:s', $current_date_num);
echo $new_current . " AFTER DATE<br/>";
$current_date_num = date('Y-m-d h:i:s', $current_date_num - 60 * 60 * 6);
echo $current_date_num ." CURRENT<br/>";
$end_date = strtotime("+1 day");
$end_date = date("Y-m-d h:i:s",$end_date);
echo $end_date ." END DATE";
1322674409 BEFORE DATE HOUR
2011-11-30 06:33:29 AFTER DATE
2011-11-30 12:33:29 CURRENT
2011-12-01 06:33:29 END DATE
I'm using an API function that returns an estimated time of arrival in hh:mm left, ie. 0:31 left until arrival.
What I'm trying to do is add the returned hh:mm to the current time, so the end result is the estimated time of arrival in UTC.
I currently have a very simple script that works as-is, but as the API function is formatted hh:mm and strtotime doesn't seem to recognize adding or subtracting anything but integers, this won't work if in the following script you replace +07 with +hh:mm.
<?php
$time = strtotime("now +07 hours");
print gmdate('H:i T', $time);
?>
So my end outcome should be hh:mm of the ETA in UTC.
A more flexible way:
<?php
function getETA($arrival, $timezone='UTC', $format='H:i T')
{
list($hours,$minutes) = explode(':', $arrival);
$dt = new DateTime('now', new DateTimeZone($timezone));
$di = new DateInterval('PT'.$hours.'H'.$minutes.'M');
$dt->add($di);
return $dt->format($format);
}
?>
Usage:
<?php
echo getETA('07:10');
echo getETA('07:10', 'America/New_York', 'h:i a T');
?>
Example Output:
23:56 UTC
07:56 pm EDT
If you change your strtotime parameter to now +07 hours, +06 minutes you should be able to add them. To get hours and minutes separate, just use explode(':', $returnedString)
$returnedString = '07:06';
$returnedTime = explode(':', $returnedString);
$time = strtotime("now +{$returnedTime[0]} hours, +{$returnedTime[1]} minutes");
// Or this
// $time = strtotime('now +' . $returnedTime[0] . ' hours, +' . $returnedTime[1] . ' minutes');
print gmdate('H:i T', $time);
<?php
$str = "17:26";
$secs = (substr($str, 0, 2) * 3600) + (substr($str, 3, 2) * 60);
echo $secs;
// Output: 62760
?>
Given that I have $num_years, how can I add it to Time() to get a time stamp $num_years hence?
strtotime("+$num_years years") will give you a timestamp $num_years years in the future.
try
echo strtotime("now + $num_years years");
Is this what you're after?
$num_years = 5;
$future = strtotime("+{$num_years} years");
echo strftime('%Y %a %d %m, %T', strtotime('+' . $num_years . ' years') );
Change strfttime format according your needs.
Check out strtotime man page http://php.net/manual/en/function.strtotime.php
Well if you looking for the timestamp for just that time then use mktime
mktime(0,0,0,0,0,$noOfYears);
http://www.php.net/manual/en/function.mktime.php
is this what you want ?
//Example to add 1 year to a date object
$num_years = 1;
$currentDate = date("Y-m-d");// current date
//Display the current date
echo "Current Date: ".$currentDate."<br>";
//Add one year to current date
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +$num_years years");
echo "Date After adding one year: ".date('l dS \o\f F Y', $dateOneYearAdded)."<br>";
I need to calculate the timestamp of exactly 7 days ago using PHP, so if it's currently March 25th at 7:30pm, it would return the timestamp for March 18th at 7:30pm.
Should I just subtract 604800 seconds from the current timestamp, or is there a better method?
strtotime("-1 week")
strtotime is your friend
echo strtotime("-1 week");
http://php.net/strtotime
echo strtotime("-1 week");
There is the following example on PHP.net
<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
// or using strtotime():
echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n";
?>
Changing + to - on the first (or last) line will get what you want.
From PHP 5.2 you can use DateTime:
$timestring="2015-03-25";
$datetime=new DateTime($timestring);
$datetime->modify('-7 day');
echo $datetime->format("Y-m-d"); //2015-03-18
Instead of creating DateTime with string, you can setTimestamp directly on object:
$timestamp=1427241600;//2015-03-25
$datetime=new DateTime();
$datetime->setTimestamp($timestamp);
$datetime->modify('-7 day');
echo $datetime->format("Y-m-d"); //2015-03-18
<?php
$before_seven_day = $date_timestamp - (7 * 24 * 60 * 60)
// $date_timestamp is the date from where you found to find out the timestamp.
?>
you can also use the string to time function for converting the date to timestamp. like
strtotime(23-09-2013);