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.
Related
I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));
I would like to convert the number to actual month. I only need a month.
I am now trying like this
$str = 8;
$anInt = intval($str);
$test = date('M',$anInt);
echo $test;
the result is JAN, I was supposed to get "AUG" as a result. I dont know why.
any idea or suggestion for me?
The date() function assumes a timestamp.
You should try this:
$test = date('M',mktime(0,0,0,$anInt,0,0);
From the docs for date and mktime:
// Prints: Your date's month is : August
echo "Your date's month is : " . date("F", mktime(0, 0, 0, $anInt, 0, 0));
Use mktime.
echo date('M', mktime(0, 0, 0, $str));
Here you are:
echo date('M',strtotime('08/01/2012'));
Or, if you want all caps:
echo strtoupper(date('M',strtotime('08/01/2012')));
There might be other approaches to this, but this was the first to come to mind
Duplicate question. Read here.
Read about date function here.
using date() you should use timestamp.
To convert date value to timestamp, use strtotime
$date = '2012-8-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);
For your problem:
$monthnumber = 4;
$date = '2012-'.$monthnumber.'-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);
Assume that i have two variables in php:
$year
$month
Then I want to make another variable:
$date
which:
$date=$year-$month-25
So, if I have 2012 in $year and 7 for $month, $date will be 2012-07-25.
Actually, I will compare it with some date in MySQL.
$year and $month are inputted by user.
anybody have a solution?
The solution either how to make $date or anything as long it can be comparred with a date in mysql.
Thanks before. ^^
You can make a unix timestamp through this:
$myDate = mktime(0, 0, 0, $month, 25, $year);
This is a pretty useful thing to have, as you can format it into all sorts of nice via:
echo date("Y-m-d", $myDate);
// Prints something like: 2012-07-25
or
echo date("l", $myDate);
// Prints something like: Monday
or
date('l jS \of F Y h:i:s A', $myDate);
// Prints something like: Monday 8th of August 2005 03:12:46 PM
You can do as follows
$complete_date = $year."-".$month."-25";
which gives you 2012-7-25
Please, read the "php manual" for concat your PHP string.
it's not
$date = $year-$month-25;
it is
$date = $year . '-' . $month . '- 25';
or
$date = $year . "-" . $month . "- 25";
but simple quote is more optimize for php string.
The solution either how to make $date or anything as long it can be
comparred with a date in mysql
The key here is use of strtotime to create and compare.
MySQL dates can be converted to integer through the use of strototime:
strtotime($mysql_date);
Then you can get time() and compare to two:
time()<>strtotime($mysql_date) // then the two dates are not equal.
You can use mktime function
$date = date('Y-m-d',mktime(0,0,0,$month,25,$year));
Well, I would use mktime to get the timestamp of the date ( http://php.net/manual/de/function.mktime.php ) and use the command unix_timestamp(date(yourfield)) in mysql to compare them.
(the date() withing unix_timestamp is only required when you save datetime values and not pure date values)
Since mysql dates are usually in this format Y-m-d by default, you can use $thedate = date('Y-m-d',mktime(0,0,0,$month,25,$year)); where $month and $year are based on the user input. Of course you have to make the user input it in the format you want by using select/lists.
I have the following timeformat:15/08/2011 12:32:23 day/month/year hour:minute:sec and I want to convert to the following format: Y-m-d H:i:s
I tried with date('Y-m-d H:i:s', strtotime($time)) but it not works. It swaps the month and the day when it's converting from string to datenum.
strtotime understands both American (mm/dd/YYYY) and European (dd-mm-YYYY or dd.mm.YYYY) formats. You are using slashes to separate day, month and year, and that's why your date is interpreted as American. To solve that, replace the slashes with dashes.
In which case, you could very simply swap the month and date from your string:
$time_string = "15/08/2011 12:32:23";
$strtotime = explode("/",$time_string);
$strtotime = implode("/",array($strtotime[1], $strtotime[0], $strtotime[2]));
echo date('Y-m-d H:i:s', strtotime($strtotime));
Working Example: http://codepad.viper-7.com/234toO
You are going to have to reparse the time. strtotime is thinking you are trying to input a string in the format of 'm/d/Y H:i:s', but you are supplying 'd/m/Y H:i:s'.
list($date, $times) = explode(' ', $time);
list($day, $month, $year) = explode('/', $date);
$newTime = date('Y-m-d H:i:s', strtotime("$month/$day/$year $times");
Replace slashes with hyphen in the date then try it will work.
$a = '07/08/2019'; // 07 is day 08 is month.
echo date('Y-m-d', strtotime($a)); //output: 2019-07-08 where 07 became month.
$a = str_replace("/","-","07/08/2019"); // 07-08-2019
echo date('Y-m-d', strtotime($a)); //2019-08-07
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)