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;
Related
I have a date $da = '18-Nov-2015'
I want month and year separate.I tried this.but it didn't work.
$month = date('F',strtotime($da));
$YEAR = date('Y',strtotime($da));
Give it try with below code:
$da = '18-Nov-2015';
$date = DateTime::createFromFormat('d-M-Y',$da);
echo $date->format("Y");
echo $date->format("F");
Note:DateTime We can create the object using arbitrary parameters like $date = DateTime::createFromFormat('d-M-Y', $weird_user_input); which can be formatted to unix timestamp or
whatever other date format We wish.
You can use the date_parse() function. It returns an array that contains the components of the date: day, month, year, hour, minute, and others.
Usage sample:
$da = '18-Nov-2015';
$dateComps = date_parse($da);
$year = $dateComps['year'];
$month = $dateComps['month'];
$day = $dateComps['day'];
//and so on, ...
use
$da=new DateTime($da);
$da->format('m-d');
hello try this hope you will get your answer what you want.
let me know if you got iy correct.
<?php
$dateValue = Date('Y-m-d');
$time=strtotime($dateValue);
$year=date("Y",$time);
$month=date("F",$time);
$date=date("d",$time);
?>
Almost you have done.This is how you can do it.
$year = date('Y', strtotime($da));
$month = date('m', strtotime($da));
F - A full textual representation of a month, such as January or March January through December
m - Numeric representation of a month, with leading zeros 01 through 12
M - A short textual representation of a month, three letters Jan through Dec
n - Numeric representation of a month, without leading zeros 1 through 12
Or else simply you can use explode method
$dateArray = explode('-', $da);
$dateArray[0] //date
$dateArray[1] //Month
$dateArray[2] //Year
Date reference : http://php.net/manual/en/function.date.php
i tried this in http://phpfiddle.org/
$da='18-Nov-2015';
$dateValue = strtotime($da);
$year = date('Y',$dateValue);
$monthName = date('F',$dateValue);
$day = date('d',$dateValue);
echo $monthName;
echo $year;
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)
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');
so currently I have 3 variables holding the day as an integer, month as a 3 letter representation of the month, and the year as 4 digits.
So for example
$day = '16';
$month = 'nov';
$year = '2013';
Now if I want to display it like
November 16th 2013
I was thinking I would have to generate a unix timestamp from my data, then use
date('F jS Y', $timestamp);
I'm not sure how to properly generate the unix timestamp though.
This will do what you want:
echo date('F jS Y', strtotime("$month $day $year"));
The key piece is the strtotime() function that will parse a date string into a timestamp for you.
Working example: http://3v4l.org/INV8d
Follow the OOP way , it's cool ;)
<?php
$day = '16';
$month = 'nov';
$year = '2013';
$newDate="$day-$month-$year";
$date_new = DateTime::createFromFormat('d-M-Y',$newDate);
echo $newformat=$date_new->format('F dS Y'); //November 16th 2013
?>
i have string like below "June 1, 2012". i need to find day for the above string.
this value return from wordpress postdate() function.
how can i do this in php?
Can't you parse the date with strtotime?
If so, you can do it like this:
<?php
date('l', strtotime('June 1, 2012'));
?>
<?php echo date('d',strtotime('June 1, 2012')); ?>
You can use a variable instead of the static date string.
$unix_timestamp = strtotime($date);
echo date('l', $unix_timestamp); // displays Monday|Tuesday|Wednesday...
$date = "June 1, 2012";
echo date('l', strtotime($date));
$timestamp= strtotime("June 1, 2012");
$day = date('l', $timestamp);
echo $day;
For more details, read strtotime and date
$date = "June 1, 2012"
$day = date('j', strtotime($date))
You can do different options in the date function to display the day (or other date parts as needed)
Although the answer is easily found, you would be best to research first:
http://uk.php.net/manual/en/function.strtotime.php