If I have a string which represents a date, like "2011/07/01" (which is 1st July 2011) , how would I output that in more readable forms, like:
1 July 2011
1 Jul 2011 (month as three letters)
And also, how could I make it intelligently show date ranges like "2011/07/01" to "2011/07/11" as
1 - 11 July 2001
(without repeating the 'July' and '2011' in this case)
You can convert your date to a timestamp using strtotime() and then use date() on that timestamp. On your example:
$date = date("j F Y", strtotime("2011/07/01")); // 1 July 2011
$date = date("j M Y", strtotime("2011/07/01")); // 1 Jul 2011
As NullUserException mentioned, you can use strtotime to convert the date strings to timestamps. You can output 'intelligent' ranges by using a different date format for the first date, determined by comparing the years, months and days:
$date1 = "2011/07/01";
$date2 = "2011/07/11";
$t1 = strtotime($date1);
$t2 = strtotime($date2);
// get date and time information from timestamps
$d1 = getdate($t1);
$d2 = getdate($t2);
// three possible formats for the first date
$long = "j F Y";
$medium = "j F";
$short = "j";
// decide which format to use
if ($d1["year"] != $d2["year"]) {
$first_format = $long;
} elseif ($d1["mon"] != $d2["mon"]) {
$first_format = $medium;
} else {
$first_format = $short;
}
printf("%s - %s\n", date($first_format, $t1), date($long, $t2));
As for the second one:
$time1 = time();
$time2 = $time1 + 345600; // 4 days
if( date("j",$time1) != date("j",$time2) && date("FY",$time1) == date("FY",$time2) ){
echo date("j",$time1)." - ".date("j F Y",$time2);
}
Can be seen in action here
Just make up more conditions
I would use strtotime AND strftime. Is a much simpler way of doing it.
By example, if a have a date string like "Oct 20 18:29:50 2001 GMT" and I want to get it in format day/month/year I could do:
$mystring = "Oct 20 18:29:50 2001 GMT";
printf("Original string: %s\n", $mystring);
$newstring = strftime("%d/%m/%Y", strtotime($mystring));
printf("Data in format day/month/year is: %s\n", $newstring);
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 convert 2 dates to timestamps. One is generated by php (today's date) and the other is input by the user
$today = date("d-m-y"); // 01-12-13
$start_date = "28-11-13";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Problem is that the result are incorrect
Result:
01-12-13 > 28-11-13 1008201600 > 1857686400
what's wrong ?
Always use four-digit years, as using two-digit years leads to endless headaches. 13 is not treated as a year; instead, 01-12-13 and 28-11-13 are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".
If you use 2013 instead, the issues disappear:
$today = date("d-m-Y"); // 01-12-2013
$start_date = "28-11-2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Output:
01-12-2013 > 28-11-2013
1385884800 > 1385625600
My understanding of the documentation is that you are trying to convert a readable string into a Unix based timestamp. When you attempt to convert 01-12-13 and 28-11-13 you will get undesired results because the strtotime function has no recollection of how to properly interpret these values. Something along the lines of the code below should help you out.
$today = date("jS M Y"); // 1st Dec 2013
$start_date = "28th Nov 2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
output
1st Dec 2013 > 28th Nov 2013
1385877600 > 1385618400
Just adjust your code to fit within the limitations of the language and if these values are coming from a form field you should be able to quickly and easily convert them to a readable date.
The function assumes that you start with a year
(check : http://www.onlineconversion.com/unix_time.htm), change to
$start_date = "28-11-2013";
This way the function knows which is the year, it takes the default foramt which is (if not misstaken) Y-d-m
I have two dates from-date & to-date.
I have to compare them from existing dates shown below, whether any of the day fall between them or not using php?
i can do for single date checking ,but i am confuse for the two date checking.
Example:
i have to check these dates:-> from=15 March 2013 & 15 April 2013 between the following dates whether any days falls in between these two date or not.
following data from db table
# from date to-date
-----------------------------------------
1 01 April 2013 30 April 2013 //here we will find as falling
2 01 May 2013 15 May 2013
3 01 June 2013 20 June 2013
Currently,in my mind not even a single logic is coming to try. Please give me any logic or suggestions regarding this issue..
The simplest way to compare dates is to convert them to a unix timestamp
Because the unix timestamp is an integer, you can simply use relational operators to compare them.
Example
// set some example data
$referenceDate = '01 April 2013';
$fromDate = '01 January 2013';
$toDate = '01 June 2013';
// convert dates to timestamps (strings to integers)
$referenceTimestamp = strtotime( $referenceDate );
$fromTimestamp = strtotime( $fromDate );
$toTimestamp = strtotime( $toDate );
// isBetween is Boolean TRUE if reference date is greater or equal fromDate and smaller or equal toDate
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
EDIT 1
To actually answer your question:
You have two ranges you need to test for overlap, this question has been answered here What's the most efficient way to test two integer ranges for overlap?
// our two ranges overlap if the following conditions are met
$dateRangeOverlaps = $referenceFromTimestamp <= $toTimestamp and $fromTimestamp <= $referenceToTimestamp;
Please try the code,
$ourdate = strtotime('1 April 2013'); // Your date which is to be checked
$from = strtotime('15 March 2013'); // From date
$to = strtotime('15 April 2013'); // To date
if ($ourdate >= $from && $ourdate <= $to)
{
echo "Date falls";
}
else
{
echo "No Date falls";
}
If you need to check several dates, pass it as an array, like below...
$i=0;
$dates= array("11 April 2013","16 April 2013");
foreach($dates as $ourdates)
{
$ourdate= strtotime($ourdates); //Your dates to be checked
$from = strtotime('15 March 2013'); // From date
$to = strtotime('15 April 2013'); // To date
if ($ourdate >= $from && $ourdate <= $to)
{
$i++;
}
}
if($i>0)
{
echo "Date falls";
}
else
{
echo "No Date falls";
}
How can I get the same day of the same ISO-8601 week number but in the previous year using php?
I am familiar with extracting this soft of information from a time stamp. Is there a built-in way for me to go from day of week, week of year and year to time stamp?
You can use the combination of both date and strtotime like so:
// get the timestamp
$ts = strtotime('today');
// get year, week number and day of week
list($year, $week, $dow) = explode('-', date('Y-W-N', $ts));
// use the "YYYY-WXX-ZZ" format
$format = ($year - 1) . "-W$week-$dow";
echo date('Y-m-d', strtotime($format)), PHP_EOL;
You can do this with strtotime:
$now = time(); // Mon, 12 Nov 2012
$targetYear = date('Y', $now) - 1; // 2011
$targetWeek = date('\\WW-N', $now); // W46-1
$lastYear = strtotime($targetYear . $targetWeek); // Mon, 14 Nov 2011
Try this one:
$date = "2012-11-13";
echo getLastYearWeek($date);
function getLastYearWeek($date)
{
$last_year = date("Y-m-d W N", strtotime("-52 Weeks ".$date));
return $last_year;
}
Or just,
$last_year = date("Y-m-d W N", strtotime("-52 Weeks ".$date));
Use this for reference.
I think you are looking for mktime. have a look here http://php.net/manual/en/function.mktime.php
Given the following timestring:
$str = '2000-11-29';
$php_date = getdate( $str );
echo '<pre>';
print_r ($php_date);
echo '</pre>';
How to get the year/month/day in PHP?
[seconds] => 20
[minutes] => 33
[hours] => 18
[mday] => 31
[wday] => 3
[mon] => 12
[year] => 1969
[yday] => 364
[weekday] => Wednesday
[month] => December
[0] => 2000
I don't know why I get 1969 for year.
Thank you
You can use strtotime to parse a time string, and pass the resulting timestamp to getdate (or use date to format your time).
$str = '2000-11-29';
if (($timestamp = strtotime($str)) !== false)
{
$php_date = getdate($timestamp);
// or if you want to output a date in year/month/day format:
$date = date("Y/m/d", $timestamp); // see the date manual page for format options
}
else
{
echo 'invalid timestamp!';
}
Note that strtotime will return false if the time string is invalid or can't be parsed. When the timestamp you're trying to parse is invalid, you end up with the 1969-12-31 date you encountered before.
PHP - How to get year, month, day from time string
$dateValue = strtotime($q);
$yr = date("Y", $dateValue) ." ";
$mon = date("m", $dateValue)." ";
$date = date("d", $dateValue);
Update: Forgot to add semicolon at end of first line, try this:
<?php
$str = "2010-08-29"; // Missed semicolon here
$time = strtotime($str);
// You can now use date() functions with $time, like
$weekday = date("l", $time); // Wednesday or whatever date it is
?>
Hopefully that will get you going!
Using the object-oriented programming style, you can do this with DateTime class
$dateFormat = 'Y-m-d';
$stringDate = '2000-11-29';
$date = DateTime::createFromFormat($dateFormat, $stringDate);
Then, you can decompose your date using the format() method
$year = $date->format('Y'); // returns a string
If you prefer the numeric format, instead of the string format, you can use the intval() function
$year = intval($date->format('Y')); // returns an integer
Here some formats that you can use
Y A full numeric representation of a year, 4 digits
m Month of the year, 2 digits with leading zeros
d Day of the month, 2 digits with leading zeros
H 24-hour format of an hour, 2 digits with leading zeros
i Minutes, 2 digits with leading zeros
s Seconds, 2 digits with leading zeros
Here the entire list of the formats that you can use : http://php.net/manual/en/function.date.php
PHP - we can get year, month, day from time string in many formats.
$dateFromDb = '2021-2-6 13:15:19';
if (($timestamp = strtotime($dateFromDb)) !== false) {
// auto format full time
$php_date = getdate($timestamp);
echo '<br>';
foreach($php_date as $key =>$value){
echo $key.'='.$value.'<br>';
}
// manually format time
echo'-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-<br>';
echo '4 digit year = '.date("Y", $timestamp).'<br>';
echo '2 digit year = '.date("y", $timestamp).'<br>';
echo 'Month = '.date("m", $timestamp).'<br>';
echo 'Day = '.date("d", $timestamp).'<br>';
echo 'Time in 24 Hours Format = '.date("H", $timestamp).'<br>';
echo 'Time in 12 Hours Format = '.date("h", $timestamp).'<br>';
echo 'Minutes = '.date("i", $timestamp).'<br>';
echo 'Seconds = '.date("s", $timestamp).'<br>';
echo '-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- <br>';
// and we can combine format also.
// and we can add '/' or ':' between year,month,days or anything we want.
echo 'Time = ' .date("H:i:s" ,$timestamp).'<br>';
echo 'Date = ' .date("d/m/Y" ,$timestamp).'<br>';
} else {
echo 'invalid date format.';
}