Jump to content

set timestamp to midnight


chiprivers

Recommended Posts

I need to set two variables for use within my pages, one containing a date as a string in format 'YYYY-MM-DD HH-MM-SS' and another containing the same date as a timestamp.

 

The date and time stored will take on the value passed in the url if available, else will be for the current date.  The time should always be midnight at the beginning of the date.

 

eg.  $datestr = '2010-09-08 00:00:00'  and  $datestamp = '1283904000'

 

Where a date is passed in the url, it will be in the format 'YYYY-MM-DD'.

 

The problem I am having is getting an accurate timestamp.  I think the trouble is connected to the handling of daylight saving time as it it always an hour out.  What I have at the present is as below:

 

<?php
// ensure all date and time functions are relative to the correct timezone (UK)
date_default_timezone_set('Europe/London');

// string holding selected / current date in format 'YYYY-MM-DD HH:MM:SS'
$datestr = (isset($_GET['date']) ? $_GET['date']." 00:00:00" : gmdate("Y-m-d")." 00:00:00");

// int holding selected / current date as unix timestamp
$datestamp = strtotime($datestr);
?>

 

Can anybody give me a simple way of converting $datestr into $datestamp to ensure that this is always correct for my UK timezone, irrespective of whether we are in or out of daylight saving time?

Link to comment
Share on other sites

Not sure that in this case it will  make any difference?  The gmdate() function, which although date() is probably sufficient, is getting the current date and outputting only the date portion, and manually adding the 00:00:00 to the string.  This part of the function is working fine, it is just that when I use strtotime() to turn the produced string into a timestamp, it is appearing to be an hour out.

 

However, I have found that it might not be out!?

 

I was checking the time stamp by putting it into an online timestamp converter to see what it converted it into but I think that may have been misleading!  When I use my script to turn my produced timestamp back into a string, it appears to do what I want it to do.  Whether it is right or not, I don't suppose it will matter so long as it converts back to what I want it to do?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.