Converting from a day of week to unix time in PHP - php

I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.

This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)

Your code works for me on PHP 5.3.0. What version of PHP are you using?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
gives:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)

Related

strototime() relative dates to the time

Currently, I have the timezone set to America/New York.
I have a timestamp of 1448933400, thats Monday, November 30, 2015 - 08:30 PM. I would like to get a relative time from this current timestamp as the "First Sun of Next Month".
So I am doing the following code:
strtotime("First Sun of Next Month", 1448933400);
The problem I am having is, is the returned value of 1449378000.
This is not at 8:30 PM, but instead at 12:00 AM. Is there an easy way to get relative dates to retain the relative time of the source timestamp?
There are many possible answers, but what you can do is first calculate the time offset for the day of your timestamp and apply it to the result:
<?php
$stamp = 1448933400;
$offset = $stamp - strtotime(date("Y-m-d", $stamp));
var_dump(strtotime("First Sun of Next Month", $stamp) + $offset);
?>
Output
int(1449451800) // or "2015-12-06 20:30:00"

Get day of the week for last 7 days?

I need the names of the day (Monday, Tuesday, Wednesday, Thuesday, Friday, Saturday, Today).
I know this is a newby question and PHP has a date() function. But I tried and can't figure out how...
According to the PHP Manual at http://php.net/manual/en/function.date.php, just use "l" as the format parameter to get the full name of the day.
So 23rd Mar 2014 is a Sunday, as echoed by
<?php
echo date ("l", mktime(0, 0, 0, 3, 23, 2014));
// Echoes Sunday
?>
To get past 7, 6, 5 or 10000 days (or number of days in the future) from the current day, according the information at this page, just use negative or positive integers in a string in the strtotime function:
<?php
$backcount = -4;
echo date ("l", strtotime("$backcount day"));
// Executed on 23 Mar 2014 will give Wednesday
?>
Knowing this, you can apply a for loop to get what you need. And if want "Today" instead of the full name of the current day, just add an if condition to handle the situation where the backcount variable is zero.
Achieving this using the DateTime Class and its format method.
The below code's output changes every day.. Since today is Sunday it starts from Monday , Tuesday... If you run this code on Tuesday , you will be getting output as Thursday , Friday , Saturday .. so on.
<?php
for($i=1;$i<=7;$i++) //<--- Since we know total days in a week is 7.
{
$date = new DateTime(); //<-- Grabs today's datetime
$date->add(new DateInterval('P'.$i.'D')); //<--- Passes the current value of $i to add days..
echo $date->format('l')."<br>";
}
OUTPUT :
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Working Demo
you can use jddayofweek to retrieve day of week, it has 3 mode for string containing the day of week, string containing the abbreviated day of week and int represent number of day in week
for($i=0;$i<7;$i++){
$x=jddayofweek($i,2);
var_dump($x);
}

trouble with php dates

I am trying to calculate, based on today's date (24 August 2012), the following three values:
Date of this month's first Saturday.
Date of this month's third Saturday.
Date of next month's first Saturday.
This is how I do it in PHP script:
// Returns August 2012
$this_month = date("F Y");
// Returns September 2012
$next_month = date("F Y", strtotime("next month"));
// Returns August 04th, Saturday
$t_sat_1st=date('U', strtotime($this_month.' First Saturday'));
// Returns August 18th, Saturday
$t_sat_3rd=date('U', strtotime($this_month.' Third Saturday'));
// Returns September 08th, Saturday
$n_sat_1st=date('U', strtotime($next_month.' First Saturday') );
Why is the wrong date returned for the last line of code? I expect it to return September 1st, 2012. What is wrong with my code?
I don't know why your code doesn't exactly work, must be the way it is parsed..
but try
$n_sat_1st=date('U', strtotime('first saturday of ' . $next_month) )
Note the 'of' it is necessary.
This code will only work in 5.3+
It should be noted that apparently some of these strings only work in PHP 5.3 apparently, notably:
"first day of this month" and "last day of this month" for example.
According to information found on another website, the "xxx day of"
feature was added in PHP 5.3.
Comment on this page - http://www.php.net/manual/en/datetime.formats.relative.php
I have tested this on both windows and ubuntu both php 5.3 and 5.4 and it works.
If this does not work, try this
$d = new DateTime($next_month);
$d->modify('first saturday of this month');
echo $d->format('U');

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

Computing relative dates in php using strtotime()

I'm looking for a reliable way to return the full date of a specified weekday (e.g. "Mon") for the current week.
Since today is Wednesday, June 13, 2012, I expected <?php echo date("Y-m-d", strtotime('Mon this week')); ?> to result in 2012-06-11, but instead php returns 2012-06-18 as though it interprets this week as meaning next week. Why this behavior and what should I be doing?
Thanks.
--Jeff
date( 'Y-m-d', strtotime( 'last Monday', strtotime( 'Sunday' ) ) );
This searches for the Monday previous to the next Sunday.
According to the documentation php relative date formats.
Then Monday this week would first advance to the next Monday and then process the relative text of this week.
dayname: Moves to the next day of this name unless it is the current day then it will not advance. In other words if the current date was June 11, then strtotime('Monday this week') would return June 11 whereas if the current date was June 13 then strtotime('Monday this week') would return June 19.
i think this is the solution for your problem:
$monday_date = date("Y-m-d", mktime(0,0,0, date("m"), date("j")-(date("w")+1), date("Y")));

Categories