Displaying textual representation of day using strtotime function in PHP - php

How to display the text representation of day like sunday monday to the desired date ... I am using the strtotime function to display the date, but how to add the text day side to the date below.
$r=27 July 2012;
echo $r;
$cmg = strtotime("$r");
$now = time(); //25 July 2012
$timeleft = $cmg-$now;
$daysleft = round((($timeleft/24)/60)/60); //probably...
echo " There are $daysleft days left!";
This outputs a date
27 July 2012
There are 2days left !
Desired output is
27 July 2012 Friday
There are 2days left!!

Put the date in this format YYYY-MM-DD then use the Date() function:
$myDate = '2012-07-25';
echo date('l', strtotime($myDate));
//Friday
Documentation: http://www.php.net/manual/en/function.date.php

Related

converting UNIX time stamp date to the Monday of that week

how can i convert a time stamp to Monday corresponding with that week?
example:
1473750000 (Tue, 13 Sep 2016 07:00:00 GMT)
// coding magic
1473638400 (Mon, 12 Sep 2016 00:00:00 GMT )
To convert timestamps, you can use a PHP Function gmdate(). I already use this on some of my projects/sites.
$timestamp = strtotime("+1 hour"); //Uk Time
echo gmdate("H:i", $timestamp); //Hour:Minutes
For the other parts of gmdate(), check the PHP Manual.
I also add my solution to the problem, using DateInterval.
function getLastMonday($timestamp){
// Initialize DateTime object
$date = new DateTime();
$date->setTimestamp($timestamp);
// Calculate num of days to substract and create date interval
$dayOfWeek = $date->format("w");
$interval = new DateInterval("P".(($dayOfWeek+6)%7).'D');
$interval->invert = 1;
// Substract the Date interval
$date->add($interval);
return $date;
}
I've found an answer using the documentation provided in the answers:
$date = date('Y-m-d',strtotime('this monday this week',1473750000));

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

how to php convert this time format?

i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php

Finding date range for current week, month and year

Suppose I have a date available with me:
2011-04-05 (i.e., 5th April, 2011)
I want to find date range for Current Week, Month and Year
Current Week: 3rd April to 9th April
Current Month: 1st April to 30 April
Current Year: 1st Jan to 31 Dec
I do understand that current year would be always 1st Jan to 31Dec, but what about current month and week, How can I find it?
Edit:
How can I find date, which is 10 days earlier or later from a given date.
Example:
Suppose today's date is 6th April, 2011
10 day's earlier: 28 March, 2011
10 day's later: 15 April, 2011
Any thoughts on this, guys?
function rangeMonth ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('Y-m-d', strtotime ('first day of this month', $dt)),
"end" => date ('Y-m-d', strtotime ('last day of this month', $dt))
);
}
function rangeWeek ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('N', $dt) == 1 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('last monday', $dt)),
"end" => date('N', $dt) == 7 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('next sunday', $dt))
);
}
print_r (rangeMonth('2011-4-5')); // format: YYYY-M-D
print_r (rangeWeek('2011-4-5'));
output for rangeMonth()
Array
(
[start] => 2011-04-01
[end] => 2011-04-30
)
output for rangeWeek()
Array
(
[start] => 2011-04-04
[end] => 2011-04-08
)
Notice: functions like getdate(), date(), etc. throw Warning if default time zone is not set in php.ini.
you can use strtotime
example :
date('d.m.Y',strtotime('last day of this month'))
date('d.m.Y',strtotime('last monday')) // for first day of this week
This solution is simple, and it takes in consideration when the current day is Monday or Sunday.
NOTE: Using strtotime('last monday') may work if the day is other than Monday, otherwise it will return the previous Monday. Because of that we should use strtotime('last monday', strtotime('tomorrow')) and it will work for any day of the current week =)
Solution:
$monday = strtotime('last monday', strtotime('tomorrow'));
$sunday = strtotime('+6 days', $monday);
echo "<P>". date('d-M-Y', $monday) . " to " . date('d-M-Y', $sunday) . "</P>";
The following code will give you the start and last date of a week:
$today = getdate();
print_r($today);
echo "<br/>";
$weekStartDate = $today['mday'] - $today['wday'];
$weekEndDate = $today['mday'] - $today['wday']+6;
echo "<br/>";
echo "<br/>";
echo "week start date:".$weekStartDate;
echo "<br/>";
echo "week end date:".$weekEndDate;
Hope it helps...
Check out the getdate function, there are a few examples of how to use it on the manual page I linked. I think it will return everything you're looking for,
Working example it properly handles the Monday issue
<?php
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Current week range from $this_week_sd to $this_week_ed ";
?>
Today is Tuesday, August 27th 2019
This week is from Monday, August 26th 2019 to Sunday, September 1st 2019
php > $date = new DateTime('Sunday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-01 00:00:00
php > $date = new DateTime('Tuesday');
php > echo $date->format('Y-m-d H:i:s');
2019-08-27 00:00:00
php > $date = new DateTime('Monday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-02 00:00:00
php > $date = new DateTime('Monday this week');
php > echo $date->format('Y-m-d H:i:s');
2019-08-26 00:00:00

How can I find a date after certain weeks?

I need to find a date after a certain number of weeks. For example, if the start date is Saturday, 31 October 2009, and if I choose 16 weeks then I need to find the date after sixteen Saturdays.
Thanks in advance.
You can use strtotime:
// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20
// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07
strtotime() is what you want: it takes an expression and turns it into a date object. Also see date():
$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)
If you're using PHP 5.2.0 or later, you can use the DateTime class
<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.
// Now add 16 weeks
$date->modify('+16 weeks');
// Now you can output it however you wish
echo $date->format('Y-m-d');
?>
Why not just use unix time, subtract 7 * 24 * 3600 * 16 from that and then convert that back into the date that you need.
This will help with the conversion back:
http://php.net/manual/en/function.date.php

Categories