PHP - number of month from date - php

I'm trying to get the number of a month from a date using this:
$newDate = date("Y-m-d", strtotime('2014-05-04'));
$Month = date('n', $newDate);
echo $Month;
It returns 1 (this is January...) How is this possible? It should return 0.
I used the date format because of that thread:
PHP: date function to get month of the date
I hope somebody can help me.
Thanks in advance
shivan

$Month = date('m', strtotime('2014-05-04'));
you can get the month like this

For datetime operation you should use DateTime class in PHP
$date = new DateTime('2014-05-04');
echo $date->format('n');

Related

PHP: Convert local monthname to monthnumber

How would one go about converting a local name of the month, to the respective month number?
You could just make the translation manually, but I'm pretty sure PHP got some built in functions for this?
$monthname = "februar"; // norwegian name for february
$monthnumber = insertCorrectFunctionHere($monthname);
The goal is for $monthnumber to become '2' here (or '02'), since february is the second month.
Just use the date() function. Use the code below
<?php
$month_number = date('m', strtotime('1 February'));
echo $month_number; //Prints 02
Another way you can do it by making an array. Use the code below
<?php
$array = array("Januar"=>"01","Februar"=>"02","Mars"=>"03","April"=>"04","Mai"=>"05","Juni"=>"06","Juli"=>"07","August"=>"08","September"=>"09","Oktober"=>"10","November"=>"11","Desember");
$month_name = "February";
$month = ucwords(strtolower("Februar"));
$month_number = $array[$month];
echo $month_number; // Prints 02
Hope this helps you
First of all you need to convert the month to time format using strtotime function
The output of strtotime function can be passed to date function to generate the month in numerical format
you can use the following code
$date = 'january';
echo date('m', strtotime($date));
The output of the above code will be 01 refering to
try like this
$date = '1 February';
$month= date('m', strtotime($date));
echo $month;

How to extract a day of the month from an RFC 3339-formatted dateTime in PHP?

I'm trying to extract just the day of the month from the dateTime formatted as follows:
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
But, when I try extracting just the day of the month, I get '31'.
I'm using: $day = date('d', $today);
Which, I'm guessing, is incorrect.
This is because the second parameter to date() needs to be a Unix Timestamp. You're passing it string. As a result you get a date of Dec 31, 1969.
All of that code is unnecessary anyways as all you need is:
$day = day('d');
If you're going to only have access to the date string you must convert it to Unix Timestamp before passing it to date().
To extract days (or other parts of a datetime), I use the format function:
$datetime = new DateTime('2000-01-10', new DateTimeZone('Pacific/Nauru'));
$day = $datetime->format('d');
echo $day;
Use any format form the PHP Manual.
Hope this will solve your problem
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
$day = date('d', strtotime($today)); // here is the difference,
// instead of $today use strtotime($today)

php get date from weeknumber and day number [duplicate]

I'm trying to get the date from the week number, day number and year.
For eg:
week number = 52
day number = 4 (of week 52)
year = 2013
In this case, the date should be 26-12-2013.
How can I do it using PHP? I've already tried with strtotime(), but I'm confused about the formats. Can someone help?
Make use of setISODate()
<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints" 26-12-2013
Try this code.
<?php
function change_date($week_num, $day) {
$timestamp = strtotime(date('Y') . '-W' . $week_num . '-' . $day);
return $timestamp;
}
$timestamp = change_date(52, 4);
echo date('d-m-Y', $timestamp);
?>
You can also use the strtotime function. In your example, you can write:
date("Y-m-d", strtotime("Y2013W52-4")) // outputs: 2013-12-26
The strtotime will give you a timestamp that you can use in combination withe the date function.

How to get the number of days of the current month?

I want to check if today is the last day of the month, but I don't really know how.
Can you help?
There is probably a more elegant solution than this but you can just use php's date function:
$maxDays=date('t');
$currentDayOfMonth=date('j');
if($maxDays == $currentDayOfMonth){
//Last day of month
}else{
//Not last day of the month
}
Try to use this:
date('t');
date('t');
or you may use
cal_days_in_month.
see here:
http://php.net/manual/en/function.cal-days-in-month.php
In order to get no. of days in month
you can use either
date('t')
OR
cal_days_in_month(CAL_GREGORIAN, 8, 2003)
And if you wish to check if today is the last day of month
us can use
if(date('d')==date('d',strtotime('last day of month'))){
//your code
}
strtotime offers many great features so check them out first
Use date function:
if (date('t') == date('j'))
{
...
}
Use php's function: cal_days_in_month
More details here
cal_days_in_month(CAL_GREGORIAN, 8, 2003);
$date = new DateTime('last day of this month');
$numDaysOfCurrentMonth = $date->format('d');
this is to find the days in any month:
$days = intval(date('t', strtotime($desiredDate)));
echo date('t'); /// it return last date of current month
And here it is, wrapped up as a function:
function is_last_day_of_month($timestamp = NULL) {
if(is_null($timestamp))
$timestamp = time();
return date('t', $timestamp) == date('j', $timestamp);
}
Using the modern DateTime object class:
$date = new DateTime('last day of this month');

Date minus 1 year?

I've got a date in this format:
2009-01-01
How do I return the same date but 1 year earlier?
You can use strtotime:
$date = strtotime('2010-01-01 -1 year');
The strtotime function returns a unix timestamp, to get a formatted string you can use date:
echo date('Y-m-d', $date); // echoes '2009-01-01'
Use strtotime() function:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
Using the DateTime object...
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
Or using now for today
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
an easiest way which i used and worked well
date('Y-m-d', strtotime('-1 year'));
this worked perfect.. hope this will help someone else too.. :)
On my website, to check if registering people is 18 years old, I simply used the following :
$legalAge = date('Y-m-d', strtotime('-18 year'));
After, only compare the the two dates.
Hope it could help someone.
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php
So, for reference, you can also use a \DateInterval to modify a \Datetime object:
$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));
echo $date->format('Y-m-d');
Which returns:
2008-01-01
For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php
You can use the following function to subtract 1 or any years from a date.
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
And looking at above examples you can also use the following
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));

Categories