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'));
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 have a database that stores dates in UTC timezone. I need to run a query to get all records with dates between the start of day of today and the end of day of today. Since the data is store in UTC and I am in EST I cannot just do y-m-d 00:00:00 to y-m-d 23:59:59 because EST is 4 hours behind UTC. How can I build this query in PHP with the correct date frame dynamically?
I also need to build a query for "last week", and "last month".
mktime works for me in most instances
<?php
$time = mysql_query("select time from timedata where timeID = '1'");
$row_time = mysql_fetch_assoc($time);
$convertedTime = strtotime($row_time['time']);
$hours = 4;
$NewTime = mktime(date('H',$convertedTime)-$hours,date('i',$convertedTime),date('s',$convertedTime),date('n',$convertedTime),date('j',$convertedTime),date('Y',$convertedTime);
echo date('Y-m-d H:i:s',$NewTime);
?>
for last week you can just take 7 off date('j') and last month take 1 off date('n')
Also be aware to set your default time zone
date_default_timezone_set('Pacific/Auckland');
http://php.net/manual/en/function.date-default-timezone-set.php
I was able to get the full day rage in EST like this:
$date_today = DateTime::createFromFormat('Y-m-d H:i:s', date('Y') . '-' . date('m') . '-' . date('d') . ' 00:00:00')->add(new DateInterval('PT4H'))->format('Y-m-d H:00:00');
$start = $date_today;
$end = DateTime::createFromFormat('Y-m-d H:i:s', date('Y') . '-' . date('m') . '-' . date('d') . ' 00:00:00')->add(new DateInterval('PT27H'))->format('Y-m-d H:59:59');
I had to create today's date with time 00:00:00 then add 4 hours ( for EST)
Then for the end date I did the same but added 27 hours and 59 min 59 sec so 3 Hr 59 Mins, 59 Sec + 24 Hr (full day)
I know that date("Y"), date("m") and date("d") will return current Year (2013), Month (07) and Date (11) respectively.
I am working with date Format: "2013-07-11". I have current date like this. Now I want to get the value "2013-06-11" and "2013-08-11" somehow using PHP.
What might be the code to get this values (Last Month's Same Date, and Next Month's Same Date)?
I tried:
$LastMonth = date ("m") - 1;
$LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d");
But this will return error when it is October. In October it will show "2013-010-11".
What can be a better solution? Can anyone help me?
Use it with PHP's strtotime():
echo date('Y-m-d', strtotime('+1 month')); //outputs 2013-08-11
echo date('Y-m-d', strtotime('-1 month')); //outputs 2013-06-11
$date = new DateTime( "2013-07-11");
$date->modify("+1 month");
echo $date->format(‘l, F jS, Y’);
Try this
$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
$nextmonth=date("dmy",strtotime("+1 month"));
$lastmonth=date("dmy",strtotime("-1 month"));
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";