Link to home
Start Free TrialLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php unix date format

Can someone please tell me how to create a unix timestamp for the following date format (strtotime or other) whe it is entered into a database:

<?php
12/10/2013

?>

And then echo the date back onto a php page in the same format from the database:
<?php

?>
I know there are no database connection details but I just want the principle please
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Here's the background information you need to understand.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

To get a Unix Timestamp you can use time() if you want now, and you may be able to use strtotime() to convert a human-readable date to an integer timestamp.

For internal representations of dates you want to use the ISO-8601 format.  It removes the ambiguity that is expressed in 12/10/2013 by reformatting into YYYY-MM-DD.  Question: which date does that expression mean?

2013-12-10
2013-10-12

This will all be much easier for you when you start using the ISO-8601 format date/time values.  Then you can define your SQL table columns in the type of DATETIME.  When you want "pretty dates" for output you can use the combination of strtotime() and date() to reformat the dates as needed.

HTH, ~Ray

PS: Check this link: http://www.laprbass.com/RAY_strtotime.php?s=12%2F10%2F2013
PHP thinks that means Tuesday 10th of December 2013 12:00:00 AM
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctorbill

ASKER

So:
This saves the date in the database:
<?php // RAY_temp_doctorbill.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors',     TRUE);
date_default_timezone_set('America/Chicago');


$str = '12/10/2013';
$tsp = strtotime($str);
$new = date('m/d/Y', $tsp);

How exactly will I use a php echo command to show it please?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All I was after was the following:

<?php echo "what goes here" $databasestringdate ?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes please
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
both
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I simply want to see a date format echoed from the saved string in the database into a php page
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have just uploaded a page called date_formats.php

I just want to be abel to enter a date value (example 21/01/2013) from the "dte1" text field into the database as a unix format
Then i just want to retreiev it  and display it as 21/01/2013

I just need a simple format

You can edit the page as appropriate
date-formats.php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,
Can't thank you enough - extremely thorough as always

One final question:
I know I am a dodo for doing this  but I have entered a lot of records into my database with the following date format (day month and year):
21/08/2012
Is it possible to convert all these dates to the correct  number string using a sql query?
The reason I ask is that when I try to carry out searches in the database based on date range I get very strange results:
example - If I use a range 21/08/2012 to 21/09/2012 I get no records but if I reverse the range (21/09/2012 - 21/08/2012) I get results
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Solved