I simply want a php function to convert date to excel number format.
Ex: 2013-11-01 to 41579
This is the way to do it in Excel
I found a way to convert a Unix timestamp to an Excel date.
$date_time = "2013-11-01 00:00:00";
$date_time_plus_one = strtotime($date_time . ' +1 day');
$str_date = strtotime(date('Y-m-d', $date_time_plus_one));
$excel_date = intval(25569 + $str_date / 86400);
echo 'php actual date time : ' . $date_time . '<br>';
echo 'add one day : ' . $date_time_plus_one . '<br>';
echo 'excel Number DATEVALUE : ' . $excel_date . '<br>';
seconds in a day: 86400 , 25569 days between 30 Dec 1899 and 01 Jan 1970. So This is the output.
php actual date time : 2013-11-01 00:00:00
add one day : 1383330600
excel Number DATEVALUE : 41579
You can change time into string like this. Every date is unique and you can also arrange them in order
$month = date("F");
$date = date("d");
$year = date("Y");
$timestamp = strtotime($month . " " . $date . " " . $year);
Related
I have the following code that is returning an unexpected answer. Please let me know what's wrong.
$start_date = new DateTime('31-03-2019');
$end_date = new DateTime('01-05-2019');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";
It is returning the following output:
day: 0 month: 1
I expect the output to be:
day: 1 month: 1
This will give you one day and one month https://3v4l.org/q0T8r
$start_date = new DateTime('31-03-2019 00:00:00');
$end_date = new DateTime('01-05-2019 24:00:00');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";
When you add 1 month to 2019-03-31, PHP will internally just increment the month value 03 to 04. The result is 2019-04-31.
As April has only 30 days, 2019-04-31 has the same meaning as 2019-05-01 has. And that's the reason, why you get one month and zero days as the result.
The DateInterval class has another handy property: days instead of m and d. It will contain the total number of days between the two dates, which equals to 31 (you have to add 31 days to 2019-03-31 to get to the 2019-05-01.
On this value you can implement your own logic, what "one month" is. If you define it as "one month = 30 days", this could be your whished result:
$start_date = new DateTime('31-03-2019');
$end_date = new DateTime('01-05-2019');
$diff = $start_date->diff($end_date);
$months = floor($diff->days / 30);
$days = $diff->days % 30;
echo "day: " . $days . " month: " . $months . "\n";
I'm trying to add some hours to a date dynamically.
I have a date, time and hours variable.
$appdate is in the format of 2013/03/23
$time is in military time format 13:00:00
$rei equals hours but it's in a plain number format from 0-48.
I'm trying to combine the $appdate and the $time together then add the $rei hours selected to it to end with $reiexpires in standard mysql format. 2013-03-23 20:00:00
$reitime = date('H',strtotime($rei));
$reidate = date('Y-m-d H:i:s',strtotime($appdate.$time));
$reiexpires = date('Y-m-d H:i:s',strtotime($reidate + $reitime));
$appdate = "2013/03/23";
$time = "13:00:00";
$rei = 48;
$reitime = strtotime($appdate . " " . $time . " + " . $rei . " hours");
$reiexpires = date('Y-m-d H:i:s', $reitime);
echo $reiexpires;
Output:
2013-03-25 13:00:00
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'
I'm trying to find the nth day of the month in a timestamp like so:
$day = 15;
$date = new DateTime('#' . $timestamp);
$date->modify($day . ' day of current month');
This generates an error:
Warning: DateTime::modify(): Failed to parse time string (15 day of current month) at position 7
I've also tried "day 15 of current month" and that does not work.
How can I modify my "modify" to find the nth day of the current month in the timestamp?
First retrieve the first day of the month, then add the number of days you want.
$date = new DateTime('first day of ' . date("Y-m-d", $timestamp));
$date->modify('+' . ($day-1) . 'days');
You can use php's date() formatting capabilities with a hard-set "day" value (adapt as necessary):
<?php
$timestamp = time();
$format = date('Y-15-M', $timestamp);
$fifteenth = strtotime($format);
echo "15th was a " . date("D", $fifteenth) . "\n";
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