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);
?>
Related
Trying to get the week number from a given date within PHP. For some reason various things I try seem to be a week off.
Looking at the following link we should be on week 3
http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php
Yet when I do the following I get week 2
echo "Weeknummer: " . date("W", strtotime("2016-01-17"));
I've also tried this code getting the same result, week 2
$date = new DateTime("2016-01-17");
$week = $date->format("W");
echo "Weeknummer: $week";
Any ideas why its seems to be a week behind and how I can fix that?
Thanks
We are, but today is the 19th (at least in my time-zone...).
You have hard-coded the 17th in your script and as you can see in the site that you mentioned, that is the last day of week 2.
The link you sent shows that 2016-01-17 is on week 2. See screenshot. In PHP the weeks start on Monday (you can read more in the docs)
It's because in mktime(), it goes like this:
mktime(hour, minute, second, month, day, year);
so try with the below code hope it's what you want
<?php
$ddate = "2016-01-17";
$duedt = explode("-", $ddate);
$date = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>
also you can try github.com/briannesbitt/Carbon to maximize your skills in Date manipulation. For carbon you can do that by:
Carbon::createFromFormat('Y-m-d H', '2012-10-18')->format('W');
I have been given year day (1-366) and I need to figure out which month it is in, how can I do this?
Well, I actually have a date string like : year, day or year, minute of day, second and I ultimately want to create a POSIX timestamp from it, how can I do this?
Thank you!
If you have PHP >= 5.3, then you can use DateTime::createFromFormat.
$day = 176;
$date = DateTime::createFromFormat('z', $day);
echo $date->getTimestamp(); // 1372275280
<?php
$year=2013;
$d=360;
echo date("m",strtotime("1/1/$year + $d days"))
?>
Use the date function to get a posix time stamp.
To get the month of a certain date, use intval(date('m'), mktime($h,$m,$s,$month,$day,$year))
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.
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 was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));