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
Related
I store user register date and time as datetime in MySQL. now to do some process with that date i need to get year, month and day separately to work with.
for example:
2015-07-30 19:20:34
now I want 2015, how do I do that in PHP?
date('Y', strtotime(<date string from mysql>));
The strtotime function (http://php.net/manual/en/function.strtotime.php) parses your date string to a Unix timestamp, and then the date function (http://php.net/manual/en/function.date.php) outputs it in the defined format.
So you can do something like:
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));
I'm not sure but try this:
<?php
$str="2015-07-30";
$explode=explode("-",$str);
echo $explode[1];
?>
$datetime = date_create("2015-07-30 19:20:34");
$day = date_format( $datetime, 'l'); // Thursday
$date = date_format( $datetime, 'd'); // 30
$month = date_format( $datetime, 'F'); // July
$year = date_format( $datetime, 'Y'); // 2015
function.date php
datetime.format.php
It is very simple, you can use strtotime() function of PHP like this:
$dated=$row['timestamp'];// in this case '2015-07-30 19:20:34';
$FullYear=date('Y',strtotime($dated)); // result 2015
$FullYear=date('y',strtotime($dated)); // result 15
date('Y', strtotime(<date string from mysql>));
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));
It should work.
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;
I a using PHP and would like the date of third day before the script is called in the format of YYYYMMDD. How can I do this?
Try this
$result = date('Y.m.d',strtotime("-3 days"));
Use DateTime:
$date = new \DateTime('-3 days');
echo $date->format('Ymd');
// Alternatively, store the string in a variable
$result = $date->format('Ymd');
try this easy way
echo date('Ymd', strtotime("-3 days"));
Atleast visit php.net once before such questions
<?php
$date = mktime(0, 0, 0, date("m") , date("d")-3, date("Y"));
echo date('Ymd', strtotime($date));
?>
I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.
How can I give this to strtotime() in a way it can convert it to a timestamp?
why convert string to date when you already know year month and date.
use setDate funtion
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
Given the variables $year $month $day $hour $minute you can do:
strtotime("$year-$month-$day $hour:$minute");
Be careful to enclose the variables with ", never in this case with '.
UPDATE (thanks to comments of #Clockwork and #Tadeck):
In case there is a variable $timeofday that represents the time of day (i.e. AM or PM),
then you can parse it this with:
strtotime("$year-$month-$day $hour:$minute$timeofday");
that is to say, just attach that variable to the end of the text.
Is strtotime the best tool for this job? What about mktime()?
$time = mktime($hour, $minute, 0, $month, $day, $year);
You can provide it to function strtotime() in many ways, as mentioned in documentation. Some examples include:
$your_time = strtotime('12/31/2011 9:59');
$your_time = strtotime('2011-12-31 9:59');
$your_time = strtotime('December 31, 2011 9:59');
etc. It really is very flexible.
You can find the list of valid formats in the documentation, and that is (from the "Compound Formats" list in the mentioned documentation) for example:
10/Oct/2000:13:55:36 -0700,
2008:08:07 18:11:31,
2008-08-07 18:11:31,
2008-07-01T22:35:17.02,
2008-07-01T22:35:17.03+08:00,
20080701T22:38:07,
20080701T9:38:07,
20080701t223807,
20080701T093807,
2008-7-1T9:3:37,
(this is really copy of the documentation)
Use it like this strtotime("YYYY-mm-DD HH:MM AM/PM"):
echo date("d F Y h:i:s A", strtotime("2011-06-01 11:15 PM")) . "\n";
OUTPUT
01 June 2011 11:15:00 PM
Y-m-d hh:mm will work
echo strtotime('2011-12-14 11:44 am');
cit #Pekka :)
strtotime($month."-".$day."-".$year)
I'm trying to convert 2010-02 to February, 2010. But, I keep getting December, 1969
I've tried using mktime, strtotime, and some combination of the two, but still haven't been able to do it...
This is what I tried most recently...
$path_title = date('F, Y', mktime(0,0,0,2,0,2010));
This would be a way to do it:
$dateString = '2010-02';
list($year, $month) = explode('-', $dateString);
$timeStamp = mktime(0, 0, 0, $month, 1, $year);
echo date('F, Y', $timestamp);
Another way would be:
$dateString = '2010-02';
$timestamp = strtotime($dateString . '-01');
echo date('F, Y', $timestamp);
strtotime can't handle ambiguous dates like "2010-02", but if you make it a full date it should work.
Otherwise, you may want to look into something like DateTime::createFromFormat.
Try this:
$str = '2010-02';
echo date('F, Y',mktime(0,0,0,substr($str,-2),1,substr($str,0,4)));
You have to make sure you use valid values to mktime(). In your example that you edited into the question, you have 0 as the day, which is effectively the first day minus one, which puts you into the previous month.