Any one got a function to like strtotime() but the takes the format as input?
For example I need to convert yyyymmdd to a timestamp, or perhaps yyyyddmm. So I would like to specify the format used. Also Im on Windows so strptime() isn't an option.
PHP 5's DateTime class has the createFromFormat method which does what you need.
However, it requires PHP 5.3 so it's not always an option (yet).
Very easy to write a function, just take a look at mktime...
// Assumes yyyy-mm-dd
function fromdate($date) {
$d = explode("-", $date);
return mktime(0, 0, 0, $d[1], $d[2], $d[0]);
}
Related
This should be simple, but I'm having trouble... so turn to StackOverflow...
I'm in the UK and am getting a date from the jQuery DatePicker in a dd/mm/yy format.
I want to store this date as a serial (yyyymmdd) so run a Date_To_Serial function that just does:
return date("Ymd", strtotime($strDate_In));
where $strDate_In is the date string in dd/mm/yy format.
So, passing in 01/12/2013, I expect 20131201 to be returned.
But, when 01/12/2013 is passed in, it returns 20130112 - PHP obviously assumes the date format is mm/dd/yyyy.
How can I fix this please?
If you know the format, try using the static createFromFormat method on the DateTime class:
$date = DateTime::createFromFormat('d/m/Y', '01/12/2013');
return $date->format('Ymd');
If the separator is a / then strtotime assumes a US-format. To have it recognise a UK format the separator must be - or .:
echo date('Ymd', strtotime('01/12/2014')); // 20140112
echo date('Ymd', strtotime('01-12-2014')); // 20141201
So for this to work in your example you would do:
return date("Ymd", strtotime(str_replace('/', '-', $strDate_In)));
Use a DateTime object's createFromFormat method. It allows you to specify the format of the input.
Afterwards, use the format method to export the date in the desired format.
Check out DateTime::createFromFormat for correct handling of non-standard date formats.
return DateTime::createFromFormat("d/m/Y", $strDate_In)->format("Ymd");
strtotime() in PHP works great if you can provide it with a date format it understands and can convert, but for example you give it a UK date it fails to give the correct unix timestamp.
Is there any PHP function, official or unofficial, that can accept a format variable that tells the function in which format the date and time is being passed?
The closest I have come to doing this is a mixture of date_parse_from_format() and mktime()
// Example usage of the function I'm after
//Like the date() function but in reverse
$timestamp = strtotimeformat("03/05/2011 16:33:00", "d/m/Y H:i:s");
If you have PHP 5.3:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '03/05/2011 16:33:00');
echo $date->getTimestamp();
You are looking for strptime, I think. you can use it to parse the date and then use mktime if you need a UNIX timestamp.
function strotimeformat($date, $format) {
$d = strptime($date, $format);
return mktime($d['tm_hour'], $d['tm_min'], $d['tm_sec'],
$d['tm_mon'], $d['tm_mday'], $d['tm_year']);
}
This will work with PHP 5.1 and onwards.
strtotime assumes it's a US date/time when using / as the separator. To get it to think it's a Euro date/time, use - or . as the date separator. You can change the /s to -s or .s with a simple str_replace()
I display the date or the time on my website a lot and I'm thinking about writing a function to parse a PostgreSQL timestamp.
The timestamp is in the format: Y-m-d H:i:s.u. E.g. 2011-04-08 23:00:56.544.
I'm thinking about something like this:
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
// parse the timestamp
return $formatted_timestamp;
}
However I am wondering whether this can also be achieved without writing a parser for it myself (with the use of some PHP function).
function parse_timestamp($timestamp, $format = 'd-m-Y')
{
return date($format, strtotime($timestamp));
}
Don't forget to set timezone before, e.g.
date_default_timezone_set('UTC');
Or in your case, I guess 'Europe/Amsterdam'.
You can always get PHP timestamp of this format Y-m-d H:i:s.u using strtotime(). Then, using date() you can export time in your own format. Both functions depend of time zone set.
strtotime is perfectly capable of parsing that time string, then just reformat it with date:
echo date('d-m-Y', strtotime('2011-04-08 23:00:56.544')); // 08-04-2011
For those using DateTime class:
DateTime::createFromFormat('Y-m-d H:i:s.u', $yourTime);
If the database isn't giving you what you want, change it. PostgreSQL can also format dates and times.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-MM-YYYY');
04-03-2011
But that's risky in international contexts, like the web. Different locales expect different ordering of elements. (Does "04-03" mean 03-April or 04-March?) This expression is easier to understand, and it uses locale-specific abbreviations for the months.
select to_char(timestamp '2011-03-04 07:04:00', 'DD-Mon-YYYY');
04-Mar-2011
Take a look at the strptime function - it can parse many time formats.
I'm currently scraping content from a website using PHP and YQL. I need to convert an awkward date format into a UNIX timestamp so it can be formatted into a MySQL compatible date. I have tried using strtotime() but to no avail. Maybe a regular expression is the answer?
Examples of dates
08Dec10
06Aug10
29Jul10
07Jun10
04May10
Dan
If you are using PHP 5.3+
Then then DateTime::createFromFormat function is perfect for your needs..
$date = DateTime::createFromFormat('dMy', '08Dec10');
Then you can do whatever you want with the date, if you need it in a different format:
echo $date->format('Y-m-d');
If you are ever able to use the DateTime functions, several available from 5.2+ and more added in 5.3 then you should. Compared to the other functional solutions, the DateTime approaches are much more readable.
Also in all solutions, whether functional or using the DateTime object, check the return values. Both createFromFormat and strptime return false on error. So you can log the error and determine what the issue was.
Note: strptime is not available on Windows.
One solution is to use strptime:
$parts = strptime($str, '%d%b%y');
and then you can pass the values to mktime:
$timestamp = mktime(0,0,0,$parts['tm_mon']+1, $parts['tm_mday'], $parts['tm_year']+1900);
Break up the date components using substr and use strtotime to put it all together again.
<?php
strtotime('20'. substr($time, 5, 2). '-'. substr($time, 2, 3). '-'. substr($time, 0, 2));
?>
I want to convert date 24/09/2010 in format dd/mm/yyyy to 2010-09-24 in format yyyy-mm-dd.
This works:
date("Y-m-d",strtotime("09/24/2010"));
But this does not:
date("Y-m-d",strtotime("24/09/2010")); // it returns '1970-01-01'
Any idea why?
according to php, the valid php formats are stated here. So basically what you gave is invalid.
Alternatively, you can use mktime, date_parse_from_format or date_create_from_format
strtotime does its best to guess what you mean when given a string, but it can't handle all date formats. In you example, it is probably thinking that you are trying to refer to the 24th month, which isn't valid, and returns 0, which date then treats as the unix epoch (the date you got).
you can get around this using the mktime() and explode() functions, like so:
$date = "24/09/2010";
$dateArr = explode("/",$date);
$timeStamp = mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]);
$newFormat = date("Y-m-d",$timeStamp);
As you say,
date("Y-m-d",strtotime("09/24/2010"))
will work,because the date format--"09/24/2010"is correct,
but "24/09/2010" is not the correct date format.
you can find something useful here