Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pre-1970 Dates

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I am trying to add form fields to enter dates and times in standard formats (or from selectors) that will convert it to a Unix timestamp, or in most cases, a NEGATIVE timestamp as many of the dates are pre-1970. Any ideas on how to accomplish this?

Something like this but which will work with both normal and negative timestamps.

Code:
int strtotime ( string $time [, int $now = time() ] );

Thank you in advance.
 
What os are you using ? What version of php? 32bit or 64 bit os?

 
The Apache Web server and PHP are the latest (or at least very recent) and I am developing on Windows 8 64-bit but for the live Linux server. I presume the live server is also 64-bit but I can't be sure without asking the hosting company.
 
OK. So shouldn't be a problem. Strtotime will work as will other date functions in php.

Pre 1970 will just be negative time stamps.

Or don't use timestamps. Just use date time objects and manipulate them with the class methods. And if you are storing them in a database do so using proper date columns. If you don't need the time part of the date time stamp consider also using julianday functions.
 
Thank you and that's what I was hoping. I do need to use the Unix time stamp but I realized after posting that, if editing the entry, the form will need to be able to show the correct date entry too. If not, then saving it would remove the info. However, my video display board apparently just died so it will be a while before I can get back to it!
 
There are windows and Linux anomalies with dates and php. Largely ironed out now.

But if your production is to be on Linux then it's sensible to develop on that platform too. Consider a droplet on digital ocean. For 5usd a month it's difficult to complain... Or if you must develop locally then use a virtual instance of your likely end architecture.

Do make sure that if using mysql you store the date time in a compliant format. Otherwise your date time comparisons might not work the way you expect. However tempting avoid storing timestamps in signed integer format (unless you are using sqlite)
 
I'm not getting notices of tek-tips replies for some reason. Anyway, the thing is that I've done this many times before with ease and good success but now it seems to complain although your original answer was what I really wanted to know just to be sure I wasn't "spinning my wheels" due to the pre-1970 dates. (I do need the time too, by the way.)

As for using Linux for development, yes, it would be preferred but some of my tools are Windows-only so I have little choice. The best I can do to get close to the production server is to use Apache as I do, rather than fighting IIS.
 
Okay, I'm back to this now but I seem to be having trouble with the times. In some cases, the timestamp (for example: -611884800) has the date only without any time but the date() function seems to show the previous day. I tried adding a second to the timestamp (-611884799) but it still shows the previous day.

Also, when there are no times I would like to do a conditional but even without any times, it seems to show 5:00 PM and on others with times they are way off!

Code:
$RawDate = $tours->ds->f("OpenDate");
$ShowTime = date("g:i A", $tours->ds->f("OpenDate"));
$StartTime = ($ShowTime != ":") ? " at $ShowTime":" Showtimes unknown";
$DocDate = ($RawDate) ? "Show date: ".date("l, F jS Y", $RawDate).$StartTime:"";

With the -611884800 timestamp, $DocDate should contain:

Show date: Friday, August 12th 1950 Showtimes unknown

but instead it has:

Show date: Friday, August 11th 1950 at 5:00 PM

The dates and times have no relation to GMT nor to any other time zone and must show as entered. Interestingly, I used an online calculator to fetch the timestamp but I just discovered that when using another calculator I get a different timestamp for the same date -611866800! It still shows the previous date but at 10:00 PM.

I'm used to using a custom function for past dates but am trying to steer away from it and use more standard PHP functions instead. In this case, it is resisting my efforts to use it! Any ideas?
 
i'm assuming that you have the proper timezone set? and that you know the timezone that the original time was published for? you will need to correct for any offset if they are not the same.

i don't know the format of the opendate node. but please try this
Code:
$ShowTime = date("g:i A", $RawDate);

note that this is likely always to give a result as $rawDate will be assumed to be zero if it is boolean false. so you might wish to check for boolean false first.

if no date part is found in the original date time string, then strototime will assume midnight in the relevant timezone. so you could also test to see whether $ShowTime == '12:00 AM'
Code:
$RawDate = $tours->ds->f("OpenDate");
$ShowTime = date("g:i A", $RawDate);
$StartTime = ($ShowTime != "12:00 AM") ? " at $ShowTime" : " Showtimes unknown";
$DocDate = ($RawDate) ? "Show date: ".date("l, F jS Y", $RawDate).$StartTime:"";
 
Thank you! I'll give it a try. As for timezone, I didn't do anything specific to set it as these dates (and sometimes times) need to show the same to site visitors anywhere in the world. What do you suggest?
 
You should ensure that a default timezone is set. In php.INI or your script as you prefer.

The choice of timezone is up to you. As to whether there will be a problem with display that largely depends on how you are storing the date and time in the database (is what format, what timezone is set in the storing script and what timezone mysql is using (Typically the same as the server's system tz which is usually UTC)
 
Thank you again. The database is storing the Unix timestamp and, of course, I have access to php.ini here on my development system but not on the live server so I'll check it first before doing it programatically. It might be that mine is local time while the server is GMT but odd the old custom function I was using seemed to present the proper dates while date() does not.
 
i see. have you checked the mysql version to ensure that it is supporting the full gamut of possible unix timestamps - you should be ok but it is worth checking?

the timestamp itself is timezone agnostic, of course, but when you convert to/from a date the timezone offset is taken into account. and depending on how you are inserting the timestamp into the database (as an integer where the conversion is done in php, or as a date where the conversion is done in mysql) you will/may get different results as the timezones that are in play may be different.

if a default time zone is not set then php will fire a warning each time a timezone dependent function is called. so if you are not seeing warnings in your error log then you should be ok. but you still need to know what timezone is in use in your scripts and in your mysql installation to know whether you need to do any corrections or overrides.

you set a default time zone in your php script like this
Code:
date_default_timezone_set('UTC'); //for utc ...

you can also set the timezone that mysql will use like this

Code:
//connect to mysql
mysql_query("set time_zone = 'CET' "); //to set to central european time

note for the above to work with mysql you MUST have populated the mysql timezone tables. this is not done automatically so because you are on a limited host you may find this difficult. you can always set the timezone using the UTC offset method

Code:
SET time_zone = '-05:00';
but you will need to compute for daylight savings etc.

to get the mysql time zone in php you can use this function

Code:
<?php 
$host = '';
$user = '';
$pass = '';

try{
	$pdo = new PDO("mysql:host={$host}", $user, $pass);
	$pdo->exec('set names utf8');
	$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}catch (PDOException $e){
	print "Error!: " . $e->getMessage() . "<br/>";
    die();
} 

function getMysqlTimeZone(){
	global $pdo;
	foreach ($pdo->query('Select @@session.time_zone as tz') as $row):
		$tz = $row->tz;
		break;// just in case
	endforeach;
	
	if($tz == 'SYSTEM'):
		foreach($pdo->query('Select @@system.time_zone as tz') as $row):
			$tz = $row->tz;
			break;
		endforeach;
	endif;
	return $tz;
}
?>

So if you wanted a programmatic method to standardise your server and mysql timezones for peace of mind you could do this as part of your connection routine.

Code:
function normaliseTimeZones($myTimesZone = 'US/Eastern'){
	/*set php */
	date_default_timezone_set($myTimeZone);
	/* get today's offset */
	$date = new datetime('now');
	$tz = new datetimezone($myTimeZone);
	$offset = $tz->getOffset();
	$sign = $offset >= 0 ? '+' : '-';
	$offset = $sign . gmdate('H:i', abs($offset));
	/* write to mysql */
	$pdo->exec("Set time_zone = '$offset'");
}

if your timezones get too messy then you might be well advised to use the date/time class in php instead of strtotime and date. they are (essentially) low level wrappers but take some of the heavy lifting away for you. e.g

Code:
date_default_timezone_set('UTC'); //you must have a default time zone set.  this can be any valid time zone
$date = new datetime;
$EST = new DateTimeZone( 'US/Eastern' );
$PST = new DateTimeZone(' US/Pacific' );
$date->setTimeStamp( $timestampFromMysql );
$date->setTimeZone( $EST );
echo $date->format( 'Y-m-d H:i A' ) . "<br/>\n";
$date->setTimeZone( $PST );
echo $date->format( 'Y-m-d H:i A' ) . "<br/>\n";

expected output would be
Code:
1969-09-03 16:52 PM
1969-09-03 13:52 PM

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top