PHP picking data from a string - php

Basically I am wondering how I would go about taking pieces of a string and putting them into another variable. I need to do this as, from API I am using, you are given the date of channel creation and I would like to take the data from this, but it is displayed in a strange way.
Here is what is given by the API:
2012-06-11T13:36:21Z seconds
^^ That is what I need to change so that I can display it in a nicer way. i.e. 11-06-2012.
Thanks.
P.S. This is twitch API if it matters.

You could use two PHP functions: "strtotime()" and "date()"
$timestamp = "2012-06-11T13:36:21Z";
echo date("jS F, Y", strtotime($timestamp));
The above would echo "11th June, 2012"

Look up the createFromFormat() function for DateTime.

Related

How to format date-time for RFC2445 in PHP

I am working on Google Calendar API using its PHP library and I need to set a recurring event for which I have to create a RRule string which should be of following format:
RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z
I am unable to create the date in above format. I literally tried all methods like:
date('YmdHis');
date(DATE_RFC2822);
date('c');
But Google doesn't accept any of above formats. I need to make it like: 20110701T170000Z. Can anybody help me on this?
Thanks
This is your date format:
date('Ymd\THis\Z')
becomes
20150429T154315Z
Look at the examples here: http://php.net/manual/de/function.date.php
Are you able to concatenate two strings together and form the required datetime string in RFC2822?
<?php
$ymd = date('Ymd');
$hms = date('His');
echo $ymd."T".$hms."Z"; //Will output YYYYMMDDTHHMMSSZ
?>
Let me know if this works! :)
Try this:
date('Ymd\THis\Z', $timestamp);
or
date('Ymd\THis\Z');
if you want the current timestamp

How to display date in specific format

I have a date field that I would like to express through a MySQL Query or as a PHP function as yyyymmdd, basically without any - or /, all the examples I can find include these.
I could build it a piece at a time, so y.m.d, but thought someone here might have come across this for, its basically a naming convention for network folders, hence why no special characters.
Thank you in advance
You can use PHP's DateTime Class:
echo (new DateTime($dateField))->format('Ymd');
SQL you can use DATE_FORMAT
DATE_FORMAT(NOW(),'%Y%m%d') //20150513
PHP you can use using date
echo date('Ymd'); //20150513
You can use strtotime() and date() function like this:
$time = strtotime($yourdatefield);
$date = date('Ymd', $time);

PHP: strtotime formatting

I am trying to put a readable time and date as part of a file name (in php). I am having all kinds of trouble with this and was hoping someone could help. I have tried several different recommendations that I have read around the internet (plus I read the manual) but I really haven't gotten anything to work right. Right now I have this:
$Time=strtotime("now");
$date=DateTime::createFromFormat('m/d/Y H:i:s', '7/24/2012 14:40:30');
$date_readable=$date->$Timestamp();
At that point I then add $date_readable to a file name. It compiles and runs but it doesn't format the date at all. It still gives it as a timestamp.
Any suggestions on how to make this work?
you can do it with simple date function for example
$time = strtotime("now");
$formatDate = date('F jS, Y h:i:s A', $time);
echo $formatDate;
this will print something like
July 25th, 2012 1:02:29 am
DateTime class is more powerful then using simple date function, as DateTime class offers powerful API's plus it is object oriented. however for simple date conversions i would stick to php's date function. as that could do my purpose.
for more formatting option have a look at this link http://www.php.net/manual/en/function.date.php#refsect1-function.date-parameters

How to change the format of timestamp in CakePHP?

In my application,I retrieve a timestamp from the table which is of the format 2009-08-18 12:09:01. I need to change this to August 18th,2009 or 18 Aug,2009. How to achieve this in CakePHP?
Are there any built in methods?
You can use the core TimeHelper to format and test dates/times.
To use it, you need to add the TimeHelper to your controller's $helpers array:
var $helpers = array('Time');
Then from the view you can format your date/time variables:
// assuming $variable['Model']['field_name'] = '2009-09-09 12:00:00':
echo $this->Time->format('F jS, Y', $variable['Model']['field_name']);
// => 'September 9th, 2009'
echo $this->Time->format('j M Y', $variable['Model']['field_name']);
// => '9 Sep 2009'
Since this method is ultimately a wrapper, use the table in the PHP documentation for the date() function to determine how to write the format string.
As of 1.3, if you reverse the order of the parameters (date first, format second) it will try to respect the date format of your locale. The above ordering will still work for backwards compatibility. More details at the bottom of this page in the migration guide.
You can use strptime to parse the string into a datetime and then strftime to format it back into the string you want.
Use the time helper as mentioned by deizel.
I would look through the file /cake/libs/view/helpers/time.php to see if there is a function there that provides the time format you are after.
The relative time functions are especially nice:
<?php echo $time->nice($dateValue); ?>
<?php echo $time->timeAgoInWords($dateValue); ?>
You can learn a great deal from reading the CakePHP source.
This should do:
<?php echo date('M js Y', strtotime($post['Post']['date'])); ?>
Here, Post is the name of the model and date is the field name.

what is the best method to translate strings to mysql dates?

What is the best way in php to take the following string mm[some char]dd[some char]yyyy and translate it to yyyymmdd?
I will probably want in the future, according to local do the same with dd[some char]mm[some char]yyyy.
If there is a way that already uses the Zend Framework API, the better
<?php
$str = '08-24-1989'; // can be in any recognizable date format.
$new_str = date('Ymd', strtotime($str)); // produces "20090824".
?>
You can replace Ymd in the second statement above with any date format characters found here.
If you're looking to use Zend's Zend_Date framework, check out some examples and documentation here. Quite frankly though, the PHP functions are a lot simpler and easier to use in your case.
date('Ymd', strtotime($time));
Strtotime is absolutely the best tool to translate almost any time format into a standard one that you can then use Date to put into the format you want.
Because you question title says MySQL Dates, this is the string format that mysql uses.
date('Y-m-d h:i:s', strtotime($time));
Unless [some char] varies , use the mysql str_to_date function, e.g. STR_TO_DATE('12|23|2009','%m|%d|%Y');
I would absolutely use TIMESTAMP for any date storage. It's incredibly easy to handle time differences (like SELECT ... WHERE date BETWEEN 2138728753 AND 376251237) and can be translated to any locale pretty easily :)

Categories