Displaying now() data in seperate day/month/year sections? - php

I am using a now() code to enter just a date into the database. The date appears as Year/Month/Day (yyyy/mm/dd).
In my PHP code I have been using:
$news_date=$row['news_date'];
to recall the date on my site, I was wondering, instead of the date all showing at once, is there a way to add a separate value for day, month and year? So I could recall 'day' separate and so forth? Obviously without changing the database to have 3 different inputs, there must be an easier way? I am fairly new to PHP and MySQL so if someone could help me out with an example that would be handy! Thanks.

PHP has a very powerful function called strtotime() that intuitively converts next to any datestamp into a UNIX-based timestamp, which you can then format any way you want.
$news_timestamp = strtotime($row['news_date']);
Now you can use PHP's date() function to output this timestamp in all sorts of ways.
print 'Today is day number '.date('d', $news_timestamp); // 20
print 'The month is '.date('F', $news_timestamp); // March
print 'Post timestamp: '.date('l, F d, Y # h:ia', $news_timestamp); // Thursday, March 20, 2014 # 03:46pm

Related

PHP Storing a date in my SQL for an appointment

This is easy and i did search for it and I gotn really complicated answers or things that didn't match my question exactly it's really simple
I have the date
$day = "Jaunuary 8th 2014 5:00pm";
I got this date from a POST from a forum, the user can only select a few dates and time so the value is controlled.
What is the best way to INSERT day and time?
like this? 04-18-2011 or 20091228 for day and what about time?
When I retrieve this information the goal is so i can sort it by time and date so that i can print it out in ORDER OF date
I should probably INSERT date and time together as one variable correct?
Why complicate things?
MySQL has the syntax of "YYYY-MM-DD HH:MM:SS" for timestamps. Luckily, PHP's date function can handle this quite well:
// the POST variable you retrieved, converted to a time via strtotime(), then converted to a date via date()
$appt = date('Y-m-d H:i:s',strtotime($_POST['appointment_datetime']));
PHP's date and strtotime functions are smart enough to interpret everything correctly and translate it to the format you need to INSERT into MySQL:
"INSERT INTO someTable (AppointmentDate) VALUES ('$appt');"
Then, when you retrieve it from the DB, you reformat it (showing the basic mysqli syntax rather than using proper binding, just for ease of explanation):
$appt = mysqli_query($link,"SELECT AppointmentDate FROM someTable;");
while($apptRow = mysqli_fetch_array($appt,MYSQL_ASSOC)){
echo date('F j, Y g:i a',strtotime($apptRow['AppointmentDate']));
}
This will echo the "plain english" version of the dates. This is obviously simplistic, you would likely capture it into a variable and display appropriately, but you should get the gist. You can consult the PHP date function documentation to learn the appropriate symbols you can use, if you want to have a different format.
Hope this helps. :)
Store a Unix Timestamp and then you can display it in any format you like
$ts=time(); // Store that value in an INT (11)
Then you can display it like
echo date("F j, Y, g:i a",$YourStoredTimestamp); // eg March 10, 2001, 5:16 pm

Changing date format globally

I am using PHP and mysql and using either Date or DateTime to save dates in mysql database. On site I have been displaying dates the way they are saved in database.
But now I want to show dates EVERYWHERE on site using one format:
April 17 2013
or
April 17 2013 12:20:50
I know I can use date and strtotime functions to display dates in above format. However there are a lot of places where I have date displaying code. So I am looking to automate the process where my current code works and displays dates in above format.
Any idea of how mysql trigger or some php magic could be created that converts all dates run through SELECT query automatically without changing my sql or php code since I have a lot of places in my code and it would be overkill to change code at all places?
For Example:
Date Saved in DB: 2013-04-16 12:41:26
SELECT QUERY: SELECT * FROM myTable
PHP: echo $row->dated; displays 2013-04-16 12:41:26
I want that without changing my php code, dates should be shown in above mentioned format globally on whole site.
Any ideas please how it could be achieved ?
You can directly format in via query using DATE_FORMAT()
SELECT DATE_FORMAT(myDate, '%M %d %Y %h:%i') myDate
FROM TableName
SQLFiddle Demo
and echo in your PHP: $row->myDate
MySQL Trigger doesn't project values and It is only fired during CrUD operations.
I would like to suggest you an alternative approach which i love to use.
You should use the epoch time. An epoch time is basicly the number of second that has passed since 1 January 1970
One if the benefits i love is that it is very easy to calculate
differences in time since you are just dealing with number of
seconds and not a complicated format such as sec min hrs
Another benefit is that it is very easy to store since its a
integer so you can store it in a sql db and have your php code understand it without worrying about the format and things like that.
In php, if you use the time() function, it will return the epoch time.
And if you ever want to display it in a user friendly way. you can use the following code:
$epoch = time();
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
As you can see, the format of the date is very flexible and thus easy to use.
A nice example to find the date 1 week ago:
$epoch = time() - 604800; //604800 seconds = 7 days
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00

How to use two date types in form?

Well basically I'm now working on contact form, and I need to add two dates types. User can specify which date they will use in a profile, and then it uses if statement to show exact date type (American or European) in form. That is easy, I can do it with if statement, but next is saving date type to database. Well basically my database table is timestamp, and saving European time is easy, but how about American time? Well basically, in my form for american time there is a hours ( 1 - 12 ), minutes ( 1 - 60) and period ( AM and PM ), well basically 3 select inputs, so any ideas, how could I convert them to 24 hour time and then save to database as European date?
Have done already everything with European date, now only American time left.
Hope you understood what I ment.
Use DateTime its can convert between any date & time format you want
$date = DateTime::createFromFormat("m/d/Y","03/32/2012");
echo $date->format("d-m-Y"); // You can use any format
OR
echo $date->format("U"); // Timestamp
If you want to use functions only
$date = date_create_from_format("m/d/Y","03/32/2012");
echo date_format($date, "d-m-Y");
For documentation on different formats : http://www.php.net/manual/en/datetime.createfromformat.php

Is there a difference in the timestamp formats used by iOS and PHP?

I got some of the timestamps from the consolidated.db on my iPhone (the one from the location tracking 'scandal' recently). I made a little PHP page to convert them to nicely formatted dates then output a list, but I'm getting dates from 1980.
Do they use a different system? Or does consolidated.db have incorrect data?
Example timestamp: 316777502
My code: $date = date("t M Y", $timestamp);
I found an article that details a manual process by which it is possible to view the data:
http://dropstones.blogspot.com/2011/04/extracting-iphone-ios4-location-data-in.html
According to that article, the timestamps are not traditional timestamps of the number of seconds from 1/1/1970, but are instead based on the number of seconds from 1/1/2001 (so, there is a 31 year offset). I cannot confirm whether this is true, but if we follow the assumption that it is, we have to add the number of seconds in 31 years (978264705) to the timestamp to change it to a traditional timestamp giving the number of seconds from 1/1/1970. The line you posted would therefore be replaced with this:
$timestamp += 978264705;
$date = date("t M Y", $timestamp);

Uniformize date formats - how to?

I have two sources of information and those sources have dates in different formats.
Source A
Wed, 17 Nov 2010 12:14:10 +0000 (from a rss feed)
Source B
YYYY-MM-DD HH:MM:SS (from mysql datetime)
Request:
I would like to order them by date to retrieve only the last occurrences.
But, and to put it even more difficult, those that are stored into the database should have in consideration the TIME. Because several records could be created on the same day.
I have control over the mysql output format to choose from.
I don't have any control about the output coming from the rss feed.
This doesn't seem to be an easy achievement, and I'm wondering:
What could you suggest here?
Update:
I was thinking about:
Working source A like this:
a) creating an array for months and the specific month number
b) Retrive two chars after the first comma (or three if we count the space) (the day)
c) Convert "Nov" to a number (by using the array previously created);
d) Retrieve the 2010 (not sure how);
e) Place the year on the left, add a - place the month, and a -, place the day, add 00:00:00
At this time, they both will be equal and that could help...
But, I feel really dummy about doing all this... :s Isn't there a smart way? :)
Over - php(zend) / mysql
Thanks a lot,
MEM
If you can use PHP 5.3, I would use DateTime::createFromFormat() to normalize the date formats into a common format.
For mySQL dates:
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2010-11-17');
echo $date->format('Y-m-d H:i:s');
For the RFC 2822 format, this should work (never tested it yet):
$date = DateTime::createFromFormat('r', 'Wed, 17 Nov 2010 12:14:10 +0000');
echo $date->format('Y-m-d H:i:s');

Categories