Jump to content

PHP: How to check if a specific date is before another specific date?

Ronson

I have a long .txt list, where each line is dated in this format: dd/mm-yy - fx. 07/09-02. All lines have different dates.

 

I want PHP to check if the date from the line is BEFORE 22-11-2004. I am unsure how to do this, as the date from the line doesn't follow any of the PHP timestamp syntaxes.

 

How can I do this?

Link to comment
Share on other sites

Link to post
Share on other sites

date() turns a unix timestamp into a formatted string. What you want to do is the opposite: turn a date/time in a specific format into a unix timestamp (which is just an integer) making it a lot easier to compare. 

 

You can try something like this: http://php.net/manual/en/function.strtotime.php

Examples: http://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php

 

If you don't know what a unix timestamp is, it's the number of seconds since January 1 1970. It's always increasing (until we reach some time in 2039 or so). 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

date() turns a unix timestamp into a formatted string. What you want to do is the opposite: turn a date/time in a specific format into a unix timestamp (which is just an integer) making it a lot easier to compare. 

 

You can try something like this: http://php.net/manual/en/function.strtotime.php

Examples: http://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php

 

If you don't know what a unix timestamp is, it's the number of seconds since January 1 1970. It's always increasing (until we reach some time in 2039 or so). 

Thanks for your answer.

 

I may be misunderstanding something. I do understand unix timestamps and the strtotime function, but that only supports certain date syntaxes. The syntax of the date in my list doesn't seem to be supported. So first I need to convert the unsupported syntax to a supported syntax - how would I do that?

 

The syntax in my list: dd/mm-yy

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for your answer.

 

I may be misunderstanding something. I do understand unix timestamps and the strtotime function, but that only supports certain date syntaxes. The syntax of the date in my list doesn't seem to be supported. So first I need to convert the unsupported syntax to a supported syntax - how would I do that?

 

The syntax in my list: dd/mm-yy

Try something like this

$date = "dd/mm-yy";$unwanted = array(":", "_", "-", ";" /* You get the point */ );$supported = str_replace($unwanted, "/", $date);// $supported == "dd/mm/yy"
Link to comment
Share on other sites

Link to post
Share on other sites

 

Try something like this

$date = "dd/mm-yy";$unwanted = array(":", "_", "-", ";" /* You get the point */ );$supported = str_replace($unwanted, "/", $date);// $supported == "dd/mm/yy"

Ofcourse, thank you. I had my head so deeply burried in converting the date that I didn't even consider just replacing chars instead.

Link to comment
Share on other sites

Link to post
Share on other sites

Ofcourse, thank you. I had my head so deeply burried in converting the date that I didn't even consider just replacing chars instead.

I know what that's like  :D

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×