Im trying to display my Date field from my MySQL database.
format is 2012-01-01 to 2012-01-31
Is there a way to display the month of January to 0 instead of 1?
I used strtotime to change its format
date('Y, n, j',strtotime($s['date']))
but this showed
2012, 1, 1
I want to display this as
2012, 0, 1
You can always do something like:
$time = strtotime($s['date']);
$str = date('Y, ', $time);
$str .= intval(date('n', $time)) - 1;
$str .= date(', j', $time);
Not sure this is the best/only way, but it should work.
EDIT: davidethell's point taken, code changed accordingly. +1 for the comment.
Or you could use getdate() instead of date() to return the date as an array, and then concatenate the required values eg.
<?php
$date = getDate(strtotime($s['date']));
echo $date['year'].', '.($date['mon']-1).', '.$date['mday'];
?>
If you prefer you can do it in SQL:
SELECT MONTH(my_date_col) - 1 AS custom_month_col;
Otherwise I agree with #Ynhockey
Edited (see comment):
SELECT CONCAT(YEAR('2012-03-08'),'-', (MONTH('2012-03-08') - 1), '-', DAY('2012-03-08')) AS custom_month_col;
In case you only need the month output, this worked for me:
$month = intval(date('n',strtotime($result["Date"]))) - 1;
$result["Date"] is how the value passed from MySQL is called.
Related
Say it I get a date from MySQL table like 2012-03-31.
I pass this date to a Java application. So Java needs that date as 2012, 02, 31.
Firstly I explode date, get second element, subtract one from the month value. Then I implode three elements and create new date string.
public function convertToJavaDate($mysqlDate) {
$pieces = explode("-",$mysqlDate);
return $pieces[0].", ".($pieces[1]-1).", ".$pieces[2];
}
Is there a quicker or smarter way to do this ?
For java see http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String, java.text.ParsePosition)
$date = strtotime('2012-03-31');
$javadata = date('Y, m, d', strtotime('-30 days', $date));
I am building a timestamp from the date, month and year values entered by users.
Suppose that the user inputs some wrong values and the date is "31-02-2012" which does not exist, then I have to get a false return. But here its converting it to another date nearby. Precisely to: "02-03-2012"..
I dont want this to happen..
$str = "31-02-2012";
echo date("d-m-Y",strtotime($str)); // Outputs 02-03-2012
Can anyone help? I dont want a timestamp to be returned if the date is not original.
You might look into checkdate.
That's because strtotime() has troubles with - since they are used to denote phrase like -1 week, etc...
Try
$str = '31-02-2012';
echo date('d-m-Y', strtotime(str_replace('-', '/', $str)));
However 31-02-2012 is not a valid English format, it should be 02-31-2012.
If you have PHP >= 5.3, you can use createFromFormat:
$str = '31-02-2012';
$d = DateTime::createFromFormat('d-m-Y', $str);
echo $d->format('d-m-Y');
You'll have to check if the date is possible before using strtotime. Strtotime will convert it to unix date meaning it will use seconds since... This means it will always be a date.
You can workaround this behavior
<?php
$str = "31-02-2012";
$unix = strtotime($str);
echo date('d-m-Y', $unix);
if (date('d-m-Y', $unix) != $str){
echo "wrong";
}
else{
echo date("d-m-Y", $unx);
}
or just use checkdate()
Use the checkdate function.
$str = "31-02-2012";
$years = explode("-", $str);
$valid_date = checkdate($years[1], $years[0], $years[2]);
Checkdate Function - PHP Manual & Explode Function - PHP Manual
Combine date_parse and checkdate to check if it's a valid time.
<?php
date_default_timezone_set('America/Chicago');
function is_valid_date($str) {
$date = date_parse($str);
return checkdate($date['month'], $date['day'], $date['year']);
}
print is_valid_date('31-02-2012') ? 'Yes' : 'No';
print "\n";
print is_valid_date('28-02-2012') ? 'Yes' : 'No';
print "\n";
Even though that date format is acceptable according to PHP date formats, it may still cause issues for date parsers because it's easy to confuse the month and day. For example, 02-03-2012, it's hard to tell if 02 is the month or the day. It's better to use the other more specific date parser examples here to first parse the date then check it with checkdate.
I want to find out the day of the week based on a databese date entry.
for example: suppose i fetch a value from the database like 03-03-1988, i want to print the day it was like monday or tuesday. how can i calculate that?
thanxx in advance..
you can do in the mysql
like
SELECT DAYOFWEEK(date) as weekday from mytable
Returns the weekday index for date (1
= Sunday, 2 = Monday, …, 7 = Saturday).
$tm = strtotime('03-03-1988');
print strftime('%A', $tm);
Be careful about strtotime, though. When feeding it "XX-XX-XXXX" it's being parsed as "DD-MM-YYYY", not "MM-DD-YYYY" as one might expect.
$myDay = date("l", mktime(0, 0, 0, 3, 3, 1988));
The result is the day you are looking for.
You need strtotime and date functions.
<?php
$d="03-03-1988";
$i= strtotime($d);
echo date("l",$i);
?>
I have a weird date format in some files I'm parsing. Here are some examples:
1954203
2012320
2010270
The first four digits are the year and the next three digits are day of year. For example, the first date is the 203rd day of 1954, or 7/22/1954.
My questions are:
What's this date format called?
Is there a pre-canned way to parse it? I don't want to reinvent the wheel here.
Edit: Sorry, I forgot to mention my language. PHP.
Try:
$oDate = DateTime::createFromFormat('Yz', 201026);
echo $oDate->format('Y-m-d');
For Java, see SimpleDateFormat. You want yyyyDDD as the format (year, then day in year).
Assuming you get it as a string in C#...
DateTime GetDate(string input)
{
int year = Int32.Parse(input.Substring(0,4));
int day = Int32.Parse(input.Substring(4,3));
return (new DateTime(year,1,1)).AddDays(day - 1);
}
(Note the -1 offset for the day number, since you are already starting at day 1)
In PHP to format the date:
echo date_parse_from_format("Yz", $date);
You can also use
DateTime::createFromFormat("YZ", $date);
or it's alias
date_create_from_format("Yz", $date)
which returns a DateTime object
Turns out what I wanted was this:
$date = '1954203';
$year = substr($date, 0, 4);
$day = substr($date, 4, 3);
$new_date = date("Y-m-d", mktime(1, 1, 1, 1, $day, $year));
I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?
Thanks,
Jonesy
I used date_format($date, "m"); //01, 02..12
This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01 just becomes 1
See http://php.net/date
date('m') or date('n') or date('F') ...
Update
m Numeric representation of a month, with leading zeros 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
F Alphabetic representation of a month January through December
....see the docs link for even more options.
What does your "data variable" look like? If it's like this:
$mydate = "2010-05-12 13:57:01";
You can simply do:
$month = date("m",strtotime($mydate));
For more information, take a look at date and strtotime.
EDIT:
To compare with an int, just do a date_format($date,"n"); which will give you the month without leading zero.
Alternatively, try one of these:
if((int)$month == 1)...
if(abs($month) == 1)...
Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.
$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime);
echo date('y', $unixtime );
as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php ) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so
echo date('n'); // "9"
As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.
<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";
// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);
// Output it month is various formats according to http://php.net/date
echo date('M',$dateAsUnixTimestamp);
// Will output Apr
echo date('n',$dateAsUnixTimestamp);
// Will output 4
echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>
To compare with an int do this:
<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
echo "They are the same";
}
?>