I would like to be able to set this start date to the beginning of the day i.e. 03/04/2023 12AM, and the end date to the end of the day i.e. 03/04/2023 11:59 PM without explicitly hard-coding the time, like I have below.
Is there a way to achieve this?
$date = '03/04/2023';
$start = strtotime($date . ' ' . '07:00');
$finish = strtotime($date . ' ' . '20:00');
Related
I am trying to add a user defined amount of months to the date previously added. $CustDate is already in YYYY-MM-DD format from previous form.
$CustDate=$_POST['formYear'] . "-" . $_POST['formMonth'] . "-" . $_POST['formDay'];
$months=$_POST['formMonthsAdded'];
$d=strtotime("+" . $months . " Months");
$CustAddedDate=date("Y-m-d", strtotime($CustDate, $d));
If I enter the date as: 2016-08-04 as the $CustDate, it gives me the same value for the $CustAddedDate.
Where am I screwing this up? Thanks!
You add the + months along with the $CustDate. Provide the $Cusdate as the second argument in the addition.
$CustAddedDate = date('Y-m-d', strtotime("+" . $months . " Months", strtotime($CustDate)));
// ^ add this with the addition
Or the DateTime variant:
$date = new DateTime($CustDate);
$date->modify('+ ' . $months . ' Month');
$CustAddedDate = $date->format('Y-m-d');
echo $CustAddedDate;
Note:
$d = strtotime('2016-03-02'); // March 2nd, 2016 -> 1456898400
echo date('Y-m-d', strtotime('+1 day', $d)); -> 2016-03-03
echo date('Y-m-d', strtotime('2010-01-02', $d)); -> 2010-01-02
The second argument for strtotime() set a time basis for any "relative" time values, like +1 day or yesterday. Since you're passing in an absolute date, 2016-08-04, there's no "relative" measure to base anything on, and your absolute date is used in its entirety for the conversion.
If you want to adjust that absolute date, you have to do something like
echo date('Y-m-d', strtotime('2016-08-04 + 1 day')) -> 2016-08-05
e.g. embed the date math into the string you're passing into strtotime, and not in the second argument.
$CustDate=$_POST['formYear'] . "-" . $_POST['formMonth'] . "-" . $_POST['formDay'];
$months=$_POST['formMonthsAdded'];
$d="+" . $months . " Months"; //not strtotime time here!
$CustAddedDate=date("Y-m-d", strtotime($d,strtotime($CustDate)));//watch the order of arguments and missing strtotime of the existing date
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);
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";
The situation
I've got two timestamps.
One with the start time and one with the end time.
The start time is: 04:43:37
The end time is: 11:59:59
Now I am trying to get the difference between the dates like this:
//define timestamps
$start_time = 1297698217;
$end_time = 1297724399;
$time_diff = $end_time - $start_time;
//display times and difference
echo
'<b>Start time:</b> ' . date('d-m-Y h:i:s', $start_time) . '<br />' .
'<b>End time:</b> ' . date('d-m-Y h:i:s', $end_time) . '<br />' .
'<b>Time difference::</b> ' . date('h:i:s', $time_diff);
The result
Start time: 14-02-2011 04:43:37
End time: 14-02-2011 11:59:59
Time difference: 08:16:22
The problem
Now the problem is that the result should be 07:16:22. I've tried this with different times, but every time I'm getting the same result. One hour too much difference.
Is there an expert willing to help?
The last one should be gmdate instead of date:
'<b>Time difference::</b> ' . gmdate('h:i:s', $time_diff);
date adjusts for your current locale, which appears to be GMT+1. gmdate does not make that adjustment.
I'm assuming that you're only planning to use this for time differences less than 24 hours.
You're asking it to turn 1970-01-01 07:16:22 into a time string for display - there are going to be differences based on your time zone.
To make it a GMT date use gmdate() but this will still cause you issues when time differences get >24 hours.
Here is what i try to get. i need the date format in php as this : 2009-11-29
$startdate = (date("Y")-1) .'-'. date("n") .'-'. (date("j")-1);
$enddate = date("Y") .'-'. date("n") .'-'. (date("j")-1);
$myAnalytics->setDateRange($startdate,$enddate);
echo 'Statistiques entre : ' . $startdate .' et ' . $enddate . '<br/><br/>';
i can get the current date, but i need to get the yesterday date (for google analytic)
so if today is 2009-11-29 removing 1 from 29 get me 28
but as today we are the 2009-12-01 i get the 0 as day...
I need to get the whole date, and remove one day, ans still get a valid date
thanks
You can use date('Y-n-j', strtotime('yesterday'));