I am trying to convert some MySQL timestamps to the official .ics format. Unfortunately, I always get the output "19700101T013334Z".
MySQL format: 2014-09-29 18:00:00
This is my PHP function, that should convert the dates accordingly:
function dateToCal($timestamp) {
return date('Ymd\THis\Z', $timestamp);
}
What am I doing wrong?
date() expects a Unix timestamp as the second parameter. You can use strtotime() to convert $timestamp to a Unix timestamp for you.
function dateToCal($timestamp) {
return date('Ymd\THis\Z', strtotime($timestamp));
}
It might be easier, and better, if you converted that date to a Unix timestamp in your query using UNIX_TIMESTAMP().
I know you've already got an answer, but you can also do this in the database:
SELECT FORMAT_DATE("2014-09-29 18:00:00", "%Y%m%dT%H%i%sZ");
The Z at the end indicates UTC so if you're storing your dates using system time you'll want to convert that as well.
You have to convert MySql's datetime to a Unix timestamp befor you can use it -
function dateToCal($timestamp) {
date('Ymd\THis', strtotime($timestamp));
}
You really do not have to have the \Z in the function.
Related
Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));
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 need to get the date/time format produced from a date / picker which currently outputs in this format 06-15-2011 09:35:32
The format I require is the same as what get produced from the function time() = 1308126939
I have look everywhere for this answer and cannot find.
use the strtotime function
echo strtotime('06-15-2011 09:35:32');
Good luck!
Try using strtotime() function which converts text datetime notation into Unix timestamp.
The timestamp from time() is a unix format representing the number of seconds since 1st January 1970. It can be replicated in date() as $timestamp = date("U");
You should be able to convert it using the strtotime() command.
$myDate = "06-15-2011 0-9:35:32"
$timestamp = strtotime($myDate)
You can then go on to format that in other ways using the date() function.
Currently I store the time in my database like so: 2010-05-17 19:13:37
However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (These two times are different)
So how could I convert the timestamp to unix timestamp? Is there a simple php function for it?
You're looking for strtotime()
You want strtotime:
print strtotime('2010-05-17 19:13:37'); // => 1274123617
Getting a unixtimestamp:
$unixTimestamp = time();
Converting to mysql datetime format:
$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);
Getting some mysql timestamp:
$mysqlTimestamp = '2013-01-10 12:13:37';
Converting it to a unixtimestamp:
$unixTimestamp = strtotime('2010-05-17 19:13:37');
...comparing it with one or a range of times, to see if the user entered a realistic time:
if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}
Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:
if(ctype_digit($_GET["UpdateTimestamp"]))
{...}
If you're using MySQL as your database, it can return date fields as unix timestamps with UNIX_TIMESTAMP:
SELECT UNIX_TIMESTAMP(my_datetime_field)
You can also do it on the PHP side with strtotime:
strtotime('2010-05-17 19:13:37');
if you store the time in the database, why don't you let the database also give you the unix timestamp of it? see UNIX_TIMESTAMP(date), eg.
SELECT UNIX_TIMESTAMP(date) ...;
databases can also do date and time comparisons and arithmetic.
I have a string as mentioned below:
$ts = "3/11/09 11:18:59 AM";
which I got using the date() function.
Now I need to convert this to a readable format like below
11-Mar-2009
I have tried everything using date(). How can I achieve this?
You need to convert it to something you can use for further formatting. strtotime() is a good start, which yields a unix timestamp. You can format that one using strftime() then.
strftime("%d-%b-%G", strtotime($ts));
Actually I tried doing this and it worked.
echo date("d-M-Y", strtotime($ts));
If you initially get the string from the date() function, then pass on formatting arguments to the date-function instead:
date('Y-m-d')
instead of converting the string once again.
EDIT: If you need to keep track of the actual timestamp, then store it as a timestamp:
// Store the timestamp in a variable. This is just an integer, unix timestamp (seconds since epoch)
$time = time();
// output ISO8601 (maybe insert to database? whatever)
echo date('Y-m-d H:i', $time);
// output your readable format
echo date('j-M-Y', $time);
Using strtotime() is convinient but unessecary parsing and storage of a timerepresentation is a stupid idea.
You can use the date() function to generate the required format directly, like so:
date("j-M-Y");
See www.php.net/date for all the possible formats of the output of the date() function.