I want to random pick a month from this month and last 3 months. Now is February'16, so the last 3 months January'16, December'15 and November'15.
Below is the php I used:
$month = mt_rand(date("m",strtotime("-3 Months")),date('m'));
However I got this error:
mt_rand(): max(2) is smaller than min(11)
because 2nd param is smaller than the 1st param. How to fix this?
I would use the unix timestamps then use the date to format the timestamp.
echo date("m", mt_rand(strtotime("-3 Months"), time()));
Format the date as you choose, http://php.net/manual/en/function.date.php.
am i to verbose with this:
$new_date =date('F Y', mktime(0, 0, 0, date("m")-rand(0,3) , date("d"), date("Y")));
echo $new_date;
Related
I know that I can use date("w"); to get a number 0-6 depending on what day it is, but how would I do this to see what day the first day in the current month is? For example, this month March 1st was a Wednesday, so it should return a 3 for Wednesday.
I tried using this date("w", "Y-m-01") but it just gives me the error
Warning: date() expects parameter 2 to be long, string given
This returns the day (eg. "Wednesday") that is first in the current month:
echo date('l',mktime(0, 0, 0, date('m'), 1));
..I suspect this was more useful to OP, hence the accept, but in fact it can be modified to meet the exact requirements of the question (number - eg "3") with:
echo date('N',mktime(0, 0, 0, date('m'), 1));
<?php
$month_start = strtotime('first day of this month', time());
//first day name , month, year
echo date('D, m Y', $month_start).'<br/>';
//first day number (Monday = 1, Tuesday = 2) etc , month, year
echo date('N, m Y', $month_start).'<br/>';
I have tried this:
$date = date("N", mktime(0, 0, 0, date('n'), 1, date('Y')));
echo $date;
you can do it with class DateTime
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2017-03-17'))
->modify('first day of this month')
->format('jS, F Y');
How would you calculate an unix timestamp of the January 1st of the current year in PHP?
What I mean by current year is that I don't want to put 2012 in it because it should be a dynamic date, next year it should be 2013 and so on.
You can use :
mktime(0, 0, 0, 1, 1, date('Y'));
You can do this:
echo strtotime('01 january ' . date('Y'));
You mean you have a date and time like this 2012-01-01 06:30:54 and you want to convert them to unix timestamp?
If that's the case then strtotime() is the best way to go buddy.
e.g : strtotime("2012-01-01 06:30:54");
or
$datetime = "2012-01-01 06:30:54";
echo strtotime($datetime);
I'm looking for the best way to get the first and the last day of a last month. I use them for make SQL queries to get stats of last month.
I think that this is the best way, more optimized but not more comprensive, anyone have another way to do the same?
$month_ini = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime("-1 month")), 1, date("Y", strtotime("-1 month"))));
$month_end = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime("-1 month")), date("t", strtotime("-1 month")), date("Y", strtotime("-1 month"))));
Thanks!!
In PHP 5.3, you can use the DateTime class :
<?php
$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");
echo $month_ini->format('Y-m-d'); // 2012-02-01
echo $month_end->format('Y-m-d'); // 2012-02-29
Last day of the previous month:
date("Y-m-d", mktime(0, 0, 0, date("m"), 0));
First day of the previous month:
date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1));
I use these in MySQL and PHP scripts, short and simple:
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
MySQL use example:
$query = $db->query("SELECT * FROM mytable WHERE 1 AND mydate >= '".date('Y-m-d', strtotime('first day of last month'))."'");
If you're doing this for the purpose of a MySQL query, have you considered using the MONTH function, e.g.
SELECT [whatever stats you're after] FROM table
WHERE MONTH(date_field) = 12 and YEAR(date_field) = 2011
This would get your stats for December. If you start to experience performance problems and the historical data doesn't change, you might want to denormalise the data into an aggregate table (rolled up by the smallest increment you need, e.g. daily/hourly/monthly etc).
you can do this in MySQL:
WHERE `DateAndTime` >= '2012-02-01 00:00:00'
AND `DateAndTime` < '2012-03-01 00:00:00'
let mysql deal with dates.
anything that is for the database to do, let it do.
like this:
mysql_query("set #last_day=last_day(now()-interval 1 month),#first_day=(#last_day-interval 1 month)+interval 1 day");
$result=mysql_query("select * from `table` where (`date` between #first_day and #last_day)");
the best is that this will work even if the year changes.
just remember to change the database timezone to match php.
I have this code, and it prints out as '01/01/1970'
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("y")-18);
echo "DOB is ".date("d/m/y", $dob);
Why is the year not 18 years less than today?
date("y") == 11 and 11-18 == -7. You need date("Y") == 2011.
Debugging tip: Print out separate parts of the code so you see what's going on. echo $dob shows that the problem is on the first line, and echo date("y")-18 tells that it's the last argument to mktime() that causes it.
This is the easiest solution :
$dob = strtotime('-18 years');
echo date('d/m/y', $dob);
strtotime() is a powerful function.
try
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")-18);
echo "DOB is ".date("d/m/y", $dob);
According to the manual, when you specify small y as the argument to date function, it will return two-digit representation of the current year. Since the current year is 2011, it will return 11. Subtracting 18 from it will give you a negative result, that's why mktime is resetting to the original timestamp.
Change date("y") to date("Y"), that is, replace small y with capital Y, then you will get the desired result.
The following code is imho much easier to read:
<?php
$dob = new DateTime();
printf("\nToday is %s", date_format($dob, 'D, M d Y'));
$dob->modify('-1 day');
$dob->modify('-18 year');
printf("\nToday minus %d day and %d year is %s",
-1, -18,
date_format($dob, 'D, M d Y'));
?>
Don't you agree? It is not so difficult to calculate date with PHP. Just look also at the PHP Date function for the various formats, such as Weeknumbers.
I'm trying to get the number of month before of the current month (now is 04 (april), so I'm trying to get 03). I'm trying this:
date('m')-1;
but I get 3. But what I want is to get 03.
The correct way to do this really is:
date('m', strtotime('-1 month'));
As you will see strange things happen in January with other answers.
The currently accepted response will result in an incorrect answer whenever the day of the month (for the current day) is a larger number than the last day of the month for the previous month.
e.g. The result of executing date('m', strtotime('-1 month')); on March 29th (in a non-leap-year) will be 03, because 29 is larger than any day of the month for February, and thus strtotime('-1 month') will actually return March 1st.
Instead, use the following:
date('n') - 1;
You may be surprised, but date() function manual page has an exact example of what you need:
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
The result of your calculation is a number. If you want to format it like a string, you can use:
$result = date('m')-1;
$string_result = sprintf("%02s", $result);
Edit: Note that this is only a partial solution to format a number like a string.
intval(date('m'))
for the current month
(intval(date('m'))-1)%12
for the previous month, also for december/january
date('m', strtotime('last month'));
This will work regardless of whether or not you're in January
This works too.
printf("%02s", (date('m') - 1));
This should do it for you...
str_pad(date('m')-1, 2, '0', STR_PAD_LEFT);